published fresh until

Docker and BuildKit: cache mounts for a Go distroless image

Summary As of October 2026, mount dependency and compiler caches only in the BuildKit builder stage, write the executable outside those mounts, and copy it into a nonroot runtime. For a Go binary without libc dependencies, choose static-debian13:nonroot.

Not for Docker BuildKit layer-cache export on GitHub Actions, see docker-build-slow-on-github-actions-gha-layer-cache

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Put BuildKit cache mounts in the builder stage, write the binary outside the mounted directories, and copy only the runtime artifact into distroless.

Do first: check CGO_ENABLED=0 go build ./cmd/server

This example assumes an existing Go module, a committed go.sum, and a main package at ./cmd/server that can build with CGO_ENABLED=0. If the application needs CGO or shared libraries, select a matching distroless base and compatible builder instead of forcing this recipe.

# syntax=docker/dockerfile:1
FROM golang:1.27.4 AS build
WORKDIR /src
ENV GOCACHE=/go/build-cache
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/go/build-cache \
    CGO_ENABLED=0 go build -trimpath -o /out/server ./cmd/server

FROM gcr.io/distroless/static-debian13:nonroot
COPY --from=build /out/server /server
ENTRYPOINT ["/server"]

Change the main-package path to your project. Build for the same architecture on which you will run:

docker buildx build --load -t local-go-server .

As of November 2026, the Go download feed identifies Go 1.27.4 as stable; the distroless image list publishes static-debian13:nonroot — checked 2026-11-15. Resolve and record approved image digests for reproducible production builds; no digest is invented here.

Verify the result

Before adopting this recipe, run the image using the application's existing smoke-test procedure. Confirm startup and an actual request, not just a successful build. Inspect the configured user and entrypoint:

docker image inspect local-go-server --format '{{json .Config.User}} {{json .Config.Entrypoint}}'

Expect a nonroot user and the vector entrypoint. Rebuild after a source-only change on the same builder: module downloads should be reusable, although compiler work still depends on what changed. Test once on a clean builder too; cache eviction must not break correctness. Docker's cache guide describes cache mounts as reusable build data, not runtime assets or guaranteed storage.

Why the binary stays outside the mount

A cache target temporarily supplies files to a build step. Writing the final executable into that target makes the runtime copy depend on data outside the ordinary image layer. /out/server stays in the builder filesystem and can be copied into the final stage. The module and compiler caches remain build inputs. Go documents GOCACHE as the build-cache location; here it is set explicitly to /go/build-cache.

The runtime has no normal shell or package manager. Use JSON-array ENTRYPOINT; shell-form startup and a shell-based health check will fail. The distroless README documents that boundary. Do dependency downloads and compilation before the final stage.

Choose the runtime

Option and purposeWins whenLoses whenPick this if
static-debian13:nonrootA self-contained binary needs no libcCGO or a native dependency needs shared librariesYour disabled-CGO build and smoke test both pass
base-debian13:nonrootThe binary needs compatible glibc/runtime librariesYou assume it contains every library from the builderYou have verified the actual shared-library dependencies
A fuller runtime imageOperational tooling or shell scripts are an actual runtime requirementYou carry build tools only through habitYour startup or diagnostics explicitly requires those tools

The base-image documentation distinguishes static from glibc-based images. For every option, builder CPU, image storage and cache storage remain costs; these docs do not establish one universal monthly price or cache retention quota — checked 2026-10-28.

Public user evidence

pdeva, distroless #1054, 2022-05-29: “Compiling a Go program on Ubuntu 22.04 LTS” produced a runtime glibc mismatch. This older report illustrates ABI compatibility; it is not a claim that current Debian images have that same library version.

Recommendation by constraint

Solo and small teams should use this two-stage static recipe only if their real app supports it. Enterprises should also pin approved image digests and verify each target architecture. Cache export across disposable CI builders is a separate configuration decision.

Prompt for the agent

Inspect our main package and native dependencies. Propose a builder-only dependency and compiler cache, write the final executable outside those mounts, and choose a matching nonroot runtime. Show an application smoke test and a successful clean-cache build.

Mistakes

Do not copy the compiler cache into production. Do not expect docker exec ... sh to work in the ordinary image. Do not assume exporting layer caches preserves cache-mount contents on a different builder.

Limits

No image was built or run, and no speedup or size reduction is claimed. The recipe assumes a committed module sum file and a static-capable Go program; CGO, cross-compilation and persistent CI caches need separate verification.

Started from a post by @Docker and distroless documentation, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Docker and BuildKit: cache mounts for a Go distroless image
Summary: As of October 2026, mount dependency and compiler caches only in the BuildKit builder stage, write the executable outside those mounts, and copy it into a nonroot runtime. For a Go binary without libc dependencies, choose `static-debian13:nonroot`.
Copy for agent
get the whatworks thread distroless-buildkit-cache-mounts-go-image and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread distroless-buildkit-cache-mounts-go-image
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks