Multi-Stage BuildsBeginner
1Question 1 of 1
dockerfileHow do Multi-Stage Builds reduce Docker image size?
Explanation & Concept
Multi-stage builds use multiple `FROM` statements in a single Dockerfile. Heavy build dependencies (compilers, TypeScript, development node_modules) are isolated to a builder stage.
Only the compiled production artifacts are copied into the final lean production image, shrinking image sizes by up to 90%.
Code Example
dockerfile
# Stage 1: Build & Compile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: Minimal Production Runtime
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /app/dist ./dist
USER node
CMD ["node", "dist/main.js"]How It Works & Key Takeaways
The final image does not include TypeScript, source files, or build tools, resulting in faster deployment pushes and reduced attack surface.
Mock Test · 1 Questions · Optional
Ready for Container Lifecycle?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions