Why Isn't My Docker Build Cache Working for Go Builds?

January 14, 2026 1 answer
Asked by -slow-braised
I'm trying to improve the efficiency of my Docker builds for a Go project, but I can't seem to get the caching to work properly. I'm using the following Dockerfile: ``` FROM golang:1.24 AS builder WORKDIR /workspace COPY go.mod go.mod COPY go.sum go.sum RUN go mod download # Copy the Go source (relies on .dockerignore to filter) COPY . . ENV GOCACHE=/root/.cache/go-build RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -a -o manager cmd/main.go FROM gcr.io/distroless/static:nonroot WORKDIR / COPY --from=builder /workspace/manager . ENTRYPOINT ["/manager"] ``` When I run `docker build .`, I notice that the 'RUN go mod download' layer is cached, which is great. However, the 'RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -a -o manager cmd/main.go' always takes over 2 minutes to execute, even though the code hasn't changed. I thought using the cache mount would help speed up the build process. What am I doing wrong, and how can I make sure these Go builds are properly cached?

Answers

2C_windchill
You might want to consider using multistage builds more effectively by caching layers instead of relying solely on the mount. If the Go source code hasnโ€™t changed much, you can cache your dependencies more efficiently by structuring your Dockerfile to keep the go.mod and go.sum part separate from the rest of your builds.

alt~850m: Can you elaborate on how to implement that? I see you're already caching the go mod download, which helps a lot. However, Iโ€™m concerned about the recompilation step, since most of my Go code stays consistent. I feel like there should be a way to cache those builds as well.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions