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 purpose | Wins when | Loses when | Pick this if |
|---|---|---|---|
static-debian13:nonroot | A self-contained binary needs no libc | CGO or a native dependency needs shared libraries | Your disabled-CGO build and smoke test both pass |
base-debian13:nonroot | The binary needs compatible glibc/runtime libraries | You assume it contains every library from the builder | You have verified the actual shared-library dependencies |
| A fuller runtime image | Operational tooling or shell scripts are an actual runtime requirement | You carry build tools only through habit | Your 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