Skip to content

Dockerfile: cache Go module + build cache to speed up go build - #27

Merged
acoshift merged 1 commit into
mainfrom
go-build-cache
Jun 25, 2026
Merged

Dockerfile: cache Go module + build cache to speed up go build#27
acoshift merged 1 commit into
mainfrom
go-build-cache

Conversation

@acoshift

Copy link
Copy Markdown
Member

What

Speed up the slow part of the image build (go build) by persisting Go's caches across builds with BuildKit cache mounts:

ENV GOCACHE=/root/.cache/go-build
ENV GOMODCACHE=/go/pkg/mod
...
RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download
...
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    go build ...

GOCACHE/GOMODCACHE are pinned via ENV so the mount targets match Go's cache dirs exactly (no dependence on the builder image's HOME/GOPATH).

Why now

Cache mounts live on the BuildKit daemon. On the persistent remote BuildKit they survive across CI runs, so only changed packages recompile and only new modules download. Build-time only — the final image is unchanged. Same change as apiserver#214; part of a workspace-wide rollout.

Note

The paths assume the public/builder image's build user is root (matches the official golang layout; no USER directive). If it runs non-root the first build fails loudly with permission denied — no latent risk. It's the same builder image across these repos, so confirming once covers them all.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UujJC8ThyuTr9EwZjnwcox

Add BuildKit --mount=type=cache for the Go module cache (/go/pkg/mod) and the
build cache (/root/.cache/go-build) on go mod download and go build. GOCACHE
and GOMODCACHE are pinned via ENV so the mount targets match Go's cache dirs
exactly, regardless of the builder image's HOME/GOPATH.

The caches persist on the BuildKit daemon across builds, so on the persistent
remote builder only changed packages recompile and only new modules download.
Cache mounts are build-time only; the final image is unchanged. Same change as
apiserver.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UujJC8ThyuTr9EwZjnwcox
@acoshift
acoshift merged commit a617416 into main Jun 25, 2026
2 checks passed
@acoshift
acoshift deleted the go-build-cache branch June 25, 2026 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant