Multi-Stage Builds in Docker
From the Docker cheat sheet ยท Dockerfile ยท verified Jul 2026
Multi-Stage Builds
Use multiple FROM stages to create small, optimized production images
dockerfile
# Build stage
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage โ only the output
FROM node:22-alpine AS production
WORKDIR /app
COPY /app/dist ./dist
COPY /app/node_modules ./node_modules
CMD ["node", "dist/server.js"]๐ก Multi-stage builds keep dev tools (compilers, build deps) out of the final image
โก Go apps can use FROM scratch for the final stage โ just the binary, ~10MB total
๐ COPY --from=stagename copies files from a previous stage into the current one
๐ข Use --target to build a specific stage: docker build --target build .
multi-stagebuildoptimization