Skip to main content

Multi-Stage Build

A multi-stage build is a Dockerfile pattern with several FROM stages, where earlier stages build the app and the final stage copies in only the output. The final image has no compilers, test tools, or source it does not need.

How it works

Each FROM starts a new stage with its own filesystem. You name stages with AS and copy artifacts between them with COPY --from=<stage>. Only the last stage becomes the final image, and earlier stages are discarded from it, though they stay in the build cache.

flowchart TD A[Stage 1: builder with full toolchain] -->|COPY --from builder| B[Stage 2: minimal runtime image] B --> C[(Final image)] classDef a fill:#FFEDD5,color:#1E293B classDef b fill:#D1FAE5,color:#1E293B class A a class B,C b

Node.js and TypeScript

Dockerfile
# syntax=docker/dockerfile:1
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS production
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --from=builder /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]

Go with a static binary

Dockerfile
FROM golang:1.23-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/payment-api ./cmd/server
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=builder /out/payment-api /payment-api
ENTRYPOINT ["/payment-api"]

Python

Dockerfile
FROM python:3.12-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /install /usr/local
COPY . .
CMD ["python", "app.py"]

Useful commands

Bash
docker build --target builder -t payment-api:builder . # build one stage
docker run --rm -it payment-api:builder sh # debug inside it

You can also copy from an external image as if it were a stage:

Dockerfile
COPY --from=nginx:1.27 /etc/nginx/nginx.conf /tmp/nginx.conf

Troubleshooting

Problem Cause Fix
File not found in final stage Wrong path in COPY --from Inspect with --target builder
Native module errors on Alpine musl versus glibc Use a -slim final base
Final image still large Copying too much Copy only the output folder
Multi-platform build fails Stage architecture mismatch Use --platform=$BUILDPLATFORM on builder
Common Mistake

COPY --from=builder /app . copies source, dev dependencies, and temp files. Copy only the build output.

Tip

Stages do not share environment variables. Redeclare ARG or ENV in each stage that needs them.

Frequently Asked Questions

What does a multi-stage build solve?

Single-stage images keep compilers, caches, and dev dependencies that the running app never needs. Multi-stage builds do the heavy work in one stage and copy only the result into a small final stage, reducing size and attack surface.

What mistake brings the bloat back?

Copying too much from the builder, such as the whole app directory or all node_modules, instead of just the built output and production dependencies.

Can I build or debug a single stage?

Yes. Use docker build --target <stage> to stop at that stage, then run it to inspect files and tools.

Found a mistake, an outdated step or a broken link?