How to Handle Go Module Cache in Docker Builds

Use a multi-stage Docker build to download Go modules in a builder stage and copy only the binary to the final image for optimal caching.

You cannot directly cache the Go module download cache (GOMODCACHE) inside a Docker build layer because the go command invalidates it based on the go.mod file hash, and Docker layers are immutable. Instead, you must use a multi-stage build to download dependencies in a builder stage and copy only the compiled binary to the final image. This approach ensures the final image is small and does not include the module cache.

FROM golang:1.23 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main .

FROM alpine:latest
COPY --from=builder /app/main /main
CMD ["/main"]