Skip to content

Commit 547ce80

Browse files
committed
dockerfile: use dockerx to cache local modules
1 parent e293ced commit 547ce80

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

CLAUDE.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ BeaconKit is a modular consensus client implementation that uses a modified Come
5151
make build # Build beacond binary to build/bin/beacond
5252
make build-docker # Build Docker image
5353
make install # Install beacond to $GOPATH/bin
54+
make clean-docker # Clean up Docker BuildX builder and cache
5455
```
5556

57+
**Docker BuildX Requirements:**
58+
- The `build-docker` target uses Docker BuildX for persistent caching and improved build performance
59+
- BuildX is included in Docker Desktop and Docker Engine 19.03+
60+
- The build process automatically creates a persistent builder instance named `beaconkit-builder`
61+
- This builder maintains cached Go modules and build artifacts across builds
62+
- To remove the builder and clear cache, run `make clean-docker`
63+
5664
### Running
5765
```bash
5866
make start # Start ephemeral devnet node (chain ID: 80087)
@@ -723,7 +731,7 @@ func ProvideBlockchainService(
723731

724732
### Dependencies
725733
- Go 1.25.3+
726-
- Docker (for running EL clients)
734+
- Docker 19.03+ with BuildX (for Docker builds and running EL clients)
727735
- Foundry (for Solidity contracts)
728736
- Make (GNU Make)
729737

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}-alpine 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}-alpine 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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,17 @@ IMAGE_NAME ?= $(TESTAPP)
100100
# Docker Paths
101101
DOCKERFILE = ./Dockerfile
102102

103-
build-docker: ## build a docker image containing `beacond`
103+
# Create buildx builder if it doesn't exist
104+
.PHONY: docker-builder-setup
105+
docker-builder-setup:
106+
@docker buildx inspect beaconkit-builder 2>/dev/null || \
107+
docker buildx create --name beaconkit-builder --driver docker-container
108+
@docker buildx use beaconkit-builder
109+
110+
build-docker: docker-builder-setup ## build a docker image containing `beacond`
104111
@echo "Build a release docker image for the Cosmos SDK chain..."
105-
docker build \
112+
docker buildx build \
113+
--load \
106114
--platform linux/$(ARCH) \
107115
--build-arg GIT_COMMIT=$(shell git rev-parse HEAD) \
108116
--build-arg GIT_VERSION=$(VERSION) \
@@ -117,3 +125,8 @@ push-docker-github: ## push the docker image to the ghcr registry
117125
@echo "Push the release docker image to the ghcr registry..."
118126
docker tag $(IMAGE_NAME):$(VERSION) ghcr.io/berachain/beacon-kit:$(VERSION)
119127
docker push ghcr.io/berachain/beacon-kit:$(VERSION)
128+
129+
clean-docker: ## clean up docker builder and cache
130+
@echo "Removing beaconkit-builder and clearing cache..."
131+
@docker buildx rm beaconkit-builder || true
132+
@echo "Docker builder cleanup complete"

0 commit comments

Comments
 (0)