Skip to content

Commit d986fe9

Browse files
committed
dockerfile: use dockerx to cache local modules
1 parent 787c467 commit d986fe9

2 files changed

Lines changed: 24 additions & 26 deletions

File tree

Dockerfile

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,7 @@ ARG DB_BACKEND=pebbledb
2727
ARG CMD_PATH=./cmd/beacond
2828

2929
#######################################################
30-
### Stage 1 - Cache Go Modules ###
31-
#######################################################
32-
33-
FROM golang:${GO_VERSION}-alpine3.20 AS mod-cache
34-
35-
WORKDIR /workdir
36-
37-
RUN apk add --no-cache git
38-
39-
# Download Go modules
40-
COPY ./go.mod ./go.sum ./
41-
RUN --mount=type=cache,target=/root/.cache/go-build \
42-
--mount=type=cache,target=/root/go/pkg/mod \
43-
go mod download
44-
45-
#######################################################
46-
### Stage 2 - Build the Application ###
30+
### Stage 1 - Build the Application ###
4731
#######################################################
4832

4933
FROM golang:${GO_VERSION}-alpine3.20 AS builder
@@ -55,13 +39,16 @@ ARG BUILD_TAGS
5539
# Set the working directory
5640
WORKDIR /workdir
5741

58-
# Consolidate RUN commands to reduce layers
42+
# Install dependencies
5943
RUN apk add --no-cache --update \
6044
ca-certificates \
6145
build-base
6246

63-
# Copy the dependencies from the cache stage
64-
COPY --from=mod-cache /go/pkg /go/pkg
47+
COPY go.mod go.sum ./
48+
49+
# Download dependencies with cache mount - this will use buildx cache
50+
RUN --mount=type=cache,target=/go/pkg/mod \
51+
go mod download
6552

6653
# Copy all the source code (this will ignore files/dirs in .dockerignore)
6754
COPY ./ ./
@@ -72,9 +59,9 @@ ARG APP_NAME
7259
ARG DB_BACKEND
7360
ARG CMD_PATH
7461

75-
# Build beacond
62+
# Build beacond with cache mounts
7663
RUN --mount=type=cache,target=/root/.cache/go-build \
77-
--mount=type=cache,target=/root/go/pkg/mod \
64+
--mount=type=cache,target=/go/pkg/mod \
7865
env NAME=${NAME} DB_BACKEND=${DB_BACKEND} APP_NAME=${APP_NAME} CGO_ENABLED=1 && \
7966
go build \
8067
-mod=readonly \
@@ -91,7 +78,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
9178
${CMD_PATH}
9279

9380
#######################################################
94-
### Stage 3 - Prepare the Final Image ###
81+
### Stage 2 - Prepare the Final Image ###
9582
#######################################################
9683

9784
FROM ${RUNNER_IMAGE}
@@ -110,4 +97,4 @@ RUN mkdir -p /root/jwt /root/kzg && \
11097
EXPOSE 26656
11198
EXPOSE 26657
11299

113-
ENTRYPOINT [ "beacond" ]
100+
ENTRYPOINT [ "beacond" ]

scripts/build/build.mk

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,20 @@ IMAGE_NAME ?= $(TESTAPP)
101101
# Docker Paths
102102
DOCKERFILE = ./Dockerfile
103103

104-
build-docker: ## build a docker image containing `beacond`
104+
# Create buildx builder if it doesn't exist
105+
.PHONY: docker-builder-setup
106+
docker-builder-setup:
107+
@if ! docker buildx ls | grep -q beaconkit-builder; then \
108+
echo "Creating beaconkit-builder for persistent cache..."; \
109+
docker buildx create --name beaconkit-builder --driver docker-container --use; \
110+
else \
111+
docker buildx use beaconkit-builder; \
112+
fi
113+
114+
build-docker: docker-builder-setup ## build a docker image containing `beacond`
105115
@echo "Build a release docker image for the Cosmos SDK chain..."
106-
docker build \
116+
docker buildx build \
117+
--load \
107118
--platform linux/$(ARCH) \
108119
--build-arg GIT_COMMIT=$(shell git rev-parse HEAD) \
109120
--build-arg GIT_VERSION=$(VERSION) \

0 commit comments

Comments
 (0)