Difficulty: Intermediate
What are multi-stage Docker builds, how do they work, and why should you use them for production images?
A multi-stage build is a Dockerfile with more than one FROM instruction, where each FROM starts a new stage. You compile or bundle the application in a heavy build stage that has compilers and dev dependencies, then use COPY --from=<stage> to copy only the finished artifacts into a small final stage. The final image ships without build tools or source code, so it is smaller, faster to pull and has a smaller attack surface.
FROM begins a fresh filesystem. Nothing carries over unless you copy it explicitly.FROM golang:1.23 AS builder and reference it later with COPY --from=builder /out/app /app.COPY --from also accepts an external image: COPY --from=nginx:alpine /etc/nginx/nginx.conf /etc/nginx/.docker build --target <name>) becomes the output image.| Concern | Single stage | Multi-stage |
|---|---|---|
| Image size | Includes compiler, dev deps, caches | Only runtime artifacts |
| Security | Shells, package managers, compilers exploitable | Minimal or distroless base, fewer CVEs |
| Pull and start time | Slower autoscaling and rollouts | Faster |
| Secrets risk | Build-time files can leak into layers | Build stage is discarded |
For a statically compiled Go service, the final stage can be scratch or gcr.io/distroless/static, giving an image of a few megabytes. A Node.js or React app commonly drops from over a gigabyte with dev dependencies to a couple of hundred megabytes, or to a tiny nginx image if it is a static frontend.
builder stage runs npm ci and npm run build; runtime stage copies dist/ and production node_modules.deps stage installs dependencies so the layer is cached independently of source changes.test stage runs unit tests; CI calls docker build --target test . and the production build never includes test tooling.build/ into nginx:alpine.COPY --from=builder /app/main ., the path must match where the builder actually wrote the file; WORKDIR matters.scratch or Alpine use CGO_ENABLED=0 or a matching base.node_modules from the builder brings dev dependencies along; run npm ci --omit=dev in a dedicated stage instead.deps, build, runtime instead of relying on numeric indexes such as --from=0, which break when stages are reordered.# syntax=docker/dockerfile:1
FROM golang:1.23 AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download # cached unless deps change
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/api
FROM gcr.io/distroless/static-debian12 AS runtime
COPY --from=builder /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]
# Build: docker build -t payments-api .
# Compare: docker images payments-api
The final image contains only the static binary on a distroless base, with no Go toolchain, shell or source code.
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
# Build only the first stage, e.g. to run tests in CI:
# docker build --target build -t web-build .
Node and node_modules exist only in the build stage; the shipped image is nginx plus static files. --target stops at a named stage.
Multi-stage Build, COPY --from, Build Target, Distroless, Image Size