From 547ce80b56ace606e387d2b7e91a225e6b61c2bd Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Sun, 6 Jul 2025 16:03:32 +0000 Subject: [PATCH] dockerfile: use dockerx to cache local modules --- CLAUDE.md | 10 +++++++++- Dockerfile | 35 +++++++++++------------------------ scripts/build/build.mk | 17 +++++++++++++++-- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index e205156ed7..96e8e78917 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -51,8 +51,16 @@ BeaconKit is a modular consensus client implementation that uses a modified Come make build # Build beacond binary to build/bin/beacond make build-docker # Build Docker image make install # Install beacond to $GOPATH/bin +make clean-docker # Clean up Docker BuildX builder and cache ``` +**Docker BuildX Requirements:** +- The `build-docker` target uses Docker BuildX for persistent caching and improved build performance +- BuildX is included in Docker Desktop and Docker Engine 19.03+ +- The build process automatically creates a persistent builder instance named `beaconkit-builder` +- This builder maintains cached Go modules and build artifacts across builds +- To remove the builder and clear cache, run `make clean-docker` + ### Running ```bash make start # Start ephemeral devnet node (chain ID: 80087) @@ -723,7 +731,7 @@ func ProvideBlockchainService( ### Dependencies - Go 1.25.3+ -- Docker (for running EL clients) +- Docker 19.03+ with BuildX (for Docker builds and running EL clients) - Foundry (for Solidity contracts) - Make (GNU Make) diff --git a/Dockerfile b/Dockerfile index f88f1a1192..c41f719581 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,23 +27,7 @@ ARG DB_BACKEND=pebbledb ARG CMD_PATH=./cmd/beacond ####################################################### -### Stage 1 - Cache Go Modules ### -####################################################### - -FROM golang:${GO_VERSION}-alpine AS mod-cache - -WORKDIR /workdir - -RUN apk add --no-cache git - -# Download Go modules -COPY ./go.mod ./go.sum ./ -RUN --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/root/go/pkg/mod \ - go mod download - -####################################################### -### Stage 2 - Build the Application ### +### Stage 1 - Build the Application ### ####################################################### FROM golang:${GO_VERSION}-alpine AS builder @@ -55,13 +39,16 @@ ARG BUILD_TAGS # Set the working directory WORKDIR /workdir -# Consolidate RUN commands to reduce layers +# Install dependencies RUN apk add --no-cache --update \ ca-certificates \ build-base -# Copy the dependencies from the cache stage -COPY --from=mod-cache /go/pkg /go/pkg +COPY go.mod go.sum ./ + +# Download dependencies with cache mount - this will use buildx cache +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download # Copy all the source code (this will ignore files/dirs in .dockerignore) COPY ./ ./ @@ -72,9 +59,9 @@ ARG APP_NAME ARG DB_BACKEND ARG CMD_PATH -# Build beacond +# Build beacond with cache mounts RUN --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/root/go/pkg/mod \ + --mount=type=cache,target=/go/pkg/mod \ env NAME=${NAME} DB_BACKEND=${DB_BACKEND} APP_NAME=${APP_NAME} CGO_ENABLED=1 && \ go build \ -mod=readonly \ @@ -91,7 +78,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ ${CMD_PATH} ####################################################### -### Stage 3 - Prepare the Final Image ### +### Stage 2 - Prepare the Final Image ### ####################################################### FROM ${RUNNER_IMAGE} @@ -110,4 +97,4 @@ RUN mkdir -p /root/jwt /root/kzg && \ EXPOSE 26656 EXPOSE 26657 -ENTRYPOINT [ "beacond" ] \ No newline at end of file +ENTRYPOINT [ "beacond" ] diff --git a/scripts/build/build.mk b/scripts/build/build.mk index 55cbe1ac82..5aa684f480 100644 --- a/scripts/build/build.mk +++ b/scripts/build/build.mk @@ -100,9 +100,17 @@ IMAGE_NAME ?= $(TESTAPP) # Docker Paths DOCKERFILE = ./Dockerfile -build-docker: ## build a docker image containing `beacond` +# Create buildx builder if it doesn't exist +.PHONY: docker-builder-setup +docker-builder-setup: + @docker buildx inspect beaconkit-builder 2>/dev/null || \ + docker buildx create --name beaconkit-builder --driver docker-container + @docker buildx use beaconkit-builder + +build-docker: docker-builder-setup ## build a docker image containing `beacond` @echo "Build a release docker image for the Cosmos SDK chain..." - docker build \ + docker buildx build \ + --load \ --platform linux/$(ARCH) \ --build-arg GIT_COMMIT=$(shell git rev-parse HEAD) \ --build-arg GIT_VERSION=$(VERSION) \ @@ -117,3 +125,8 @@ push-docker-github: ## push the docker image to the ghcr registry @echo "Push the release docker image to the ghcr registry..." docker tag $(IMAGE_NAME):$(VERSION) ghcr.io/berachain/beacon-kit:$(VERSION) docker push ghcr.io/berachain/beacon-kit:$(VERSION) + +clean-docker: ## clean up docker builder and cache + @echo "Removing beaconkit-builder and clearing cache..." + @docker buildx rm beaconkit-builder || true + @echo "Docker builder cleanup complete"