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 --from=build /app/dist ./dist
COPY --from=build /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

More Docker tasks

Back to the full Docker cheat sheet