Back to the blog

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.sum don't change, go mod download uses 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 RUN line compiles:
    • CGO_ENABLED=0 disables 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=linux guarantees the binary targets Linux (useful when compiling on Mac/Windows).
    • -o /app places the executable at /app.
    • ./cmd/server is the project's main package (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 alpine starts a new, lean image.
  • Creates/uses a non-root user (appuser) — a security practice.
  • COPY --from=build /app /app copies the compiled binary from the build stage.
  • EXPOSE 8080 documents the port.
  • ENTRYPOINT defines 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 alpine image may need ca-certificates for HTTPS requests. On scratch/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/cc or distroless/static are great options for production.

Cache and speed

  • The Dockerfile above already uses the COPY go.mod go.sum before COPY . 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 build and docker image ls, or du -h on the binary. Stripping and ldflags "-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/sh won't work if the final image only has the binary. Use a debug stage, run the build image directly, or create a debug image with bash installed.