multi-stage Docker builds Explained
What multi-stage Docker builds does, with an example.
Published November 10, 2026
Multi-stage builds use several FROM stages in one Dockerfile: build with a full toolchain, then copy only the results into a small runtime image. The final image is smaller and safer.
How it works
Name stages with AS and copy artefacts using COPY --from=. Only the last stage ends up in the image.
This keeps compilers, test tools and source out of production.
Try it
docker build -t app .
Stage 1: node:20 (build, 1.1GB)
Stage 2: nginx:alpine (final, 45MB)
Practical examples
Frontend build then serve
FROM node:20 AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM nginx:alpine
COPY --from=build /app/out /usr/share/nginx/htmlGo static binary
FROM golang:1.23 AS b
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /app
FROM scratch
COPY --from=b /app /app
CMD ["/app"]Build a specific stage
docker build --target build -t app:build .Compare sizes
docker images appCommon mistakes
- Copying too much from the build stage.
- Forgetting runtime dependencies in the final stage.
- Leaving secrets in intermediate stages used in history.
Key takeaways
- Multi-stage builds use several FROM stages in one Dockerfile: build with a full toolchain, then copy only the results into a small runtime image.
- Run docker <command> --help for all options.
- Clean up unused resources regularly.
FAQ
Do earlier stages ship?
No, only the final stage becomes the image.
Where can I see all options?
Run the command with --help.