Docker multi-stage build for a Go API
dockergodevops
A multi-stage Dockerfile for compiling and packaging a Go API: a build stage with the Go toolchain, and a minimal final stage that only carries the compiled binary, ready for production.
# build stage
FROM golang:1.21-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app ./cmd/server
# final stage
FROM alpine:3.18
RUN adduser -D appuser
USER appuser
COPY --from=build /app /app
EXPOSE 8080
ENTRYPOINT ["/app"]
Build stage vs final stage (multi-stage build)
- Build stage is where the code compiles: it has all the necessary tools (Go toolchain, dependencies, compilation).
- Final stage only takes the compiled result (the binary) and creates the image that goes to production, usually minimal, secure and small.
Advantages of multi-stage builds
- Much smaller final image (no toolchain, cache, sources, etc).
- Smaller attack surface (fewer packages, fewer vulnerabilities).
- Separates responsibilities (compiling vs running).
- Makes reproducibility and dependency caching easier.
Line-by-line explanation of the structure
# build stage
FROM golang:1.21-alpine AS build
Base image for compiling. Has Go installed. AS build gives the stage a name so it can be referenced later.
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
- Copies only the dependency files first to take advantage of caching: when
go.mod/go.sumdon't change,go mod downloaduses the cache and Docker doesn't rerun the expensive steps. - This speeds up iterative builds.
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app ./cmd/server
COPY . .copies the source code.- The
RUNline compiles:CGO_ENABLED=0disables CGO — it forces Go to produce a static binary (no C dependencies). Makes it easy to run on distroless/scratch/Alpine images without extra libraries.GOOS=linuxguarantees the binary targets Linux (useful when compiling on Mac/Windows).-o /appplaces the executable at/app../cmd/serveris the project'smainpackage (should be adjusted to fit the project's needs).
# final stage
FROM alpine:3.18
RUN adduser -D appuser
USER appuser
COPY --from=build /app /app
EXPOSE 8080
ENTRYPOINT ["/app"]
FROM alpinestarts a new, lean image.- Creates/uses a non-root user (
appuser) — a security practice. COPY --from=build /app /appcopies the compiled binary from thebuildstage.EXPOSE 8080documents the port.ENTRYPOINTdefines the execution command.
Important technical notes
CGO_ENABLED=0
- Makes Go link statically. Good for using minimal images.
- If some C library is needed (e.g., SQLite with cgo), you can't disable CGO without losing functionality.
Alpine + static binary
- With
CGO_ENABLED=0, a static binary generally runs fine on Alpine. - If the binary uses TLS/CA, the
alpineimage may needca-certificatesfor HTTPS requests. Onscratch/distroless, you sometimes need to install CA certs or use variants that already have them.
Even smaller alternatives
FROM scratch(an empty image) is the smallest possible — it only works if the binary is truly static and doesn't need certs/LD_LIBRARY.gcr.io/distroless/ccordistroless/staticare great options for production.
Cache and speed
- The Dockerfile above already uses the
COPY go.mod go.sumbeforeCOPY .technique — that's ideal. - For even faster builds in CI, use BuildKit with module cache mounts (
--mount=type=cache) or Go module caching.
Binary size
- Check it with
docker buildanddocker image ls, ordu -hon the binary. Stripping andldflags "-s -w"reduce the size:RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o /app ./cmd/server
Local debugging
- To debug on your machine:
docker run --rm -it my-api:dev /bin/shwon't work if the final image only has the binary. Use a debug stage, run thebuildimage directly, or create a debug image withbashinstalled.