Skip to content
StackDecode
IntermediateLesson 14 of 26

Multi-stage builds

Compile in one image, ship only the result in a small one.

dockerfile
FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /app ./cmd/server

FROM gcr.io/distroless/static-debian12
COPY --from=build /app /app
USER nonroot
ENTRYPOINT ["/app"]

The final image has no compiler, no source and no package manager: smaller, faster to pull and with less attack surface. The same pattern works for Node (build, then serve static files) and Python (build wheels, copy them).