📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Docker Multi-Stage Builds

Multi-Stage Builds

5 min read Quiz at the end
Use multi-stage builds to produce tiny production images by discarding build-time dependencies.

Multi-Stage Builds

Build in a large image, copy only the output to a tiny final image.

# Stage 1: Build
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Production (25MB vs 800MB)
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

# Target specific stage
docker build --target builder -t myapp:dev .
Topic Quiz · 2 questions

Test your understanding before moving on

1. What is the main benefit of multi-stage builds?
💡 Multi-stage builds let you use a large build image but ship only the compiled output in a tiny image.
2. How do you copy a file from a previous build stage?
💡 COPY --from=stageName copies files from a named previous stage into the current stage.