Docker Multi-Stage Builds: Optimization Patterns & Size Reduction
Multi-stage builds are the most effective way to reduce Docker image size and attack surface. By separating build-time dependencies from runtime requirements, images can shrink 5-20x while improving security and deploy speed.
Related Products
on GumroadDeveloper Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →AI Prompt Engineering Toolkit
58 structured prompt templates for Claude, ChatGPT, and Gemini — organized by use case. Code Review, Debugging, Documentation, Architecture, and 10 model-specific tips. Stop writing prompts from scratch.
Buy on Gumroad →Developer Cheat Sheet Bundle
10 print-ready cheat sheets for the tools developers use every day: Git, Docker, Linux CLI, SQL, Regex, HTTP Status codes, VS Code shortcuts, Bash scripting, jq/JSON, and Markdown.
Buy on Gumroad →You Might Also Need
JSON Formatter & Validator
Format, beautify, and validate JSON online. Paste your JSON to instantly pretty-print it with 2-space, 4-space, or tab indentation. Minify JSON too. Free, runs in your browser.
Open free tool →
Regex Tester
Free regex tester. Test regular expressions against multiple strings with expected match/no-match assertions — see instant pass/fail results. Built-in presets for email, URL, IPv4, and phone. Pro: unl
Open free tool →
Base64 Encoder & Decoder
Free Base64 Encoder & Decoder. Convert text, images, and any file to Base64 and back. Supports URL-safe Base64, image data URIs, and file download from Base64. Runs 100% in your browser — no data sent
Open free tool →
More Free Tools
Related Articles
curl 深度使用手冊:API 測試從入門到進階
curl 是每位開發者必備的 CLI 工具。本文涵蓋 GET/POST/PUT/DELETE 請求、認證、header 設定、檔案上傳、除錯技巧,以及 REST API 測試的完整實戰範例。
Best AI Code Review Tools 2026: CodeRabbit, Sourcery, DeepCode & Bito Compared
Compare top AI code review tools for 2026: CodeRabbit, Sourcery, DeepCode, and Bito. Features, pricing, GitHub/GitLab integration, and choosing the right tool for your team.
AI Code Review Tools 2026: GitHub Copilot, CodeRabbit, Qodo Merge Ranked
Compare AI code review tools for 2026: GitHub Copilot code review, CodeRabbit, Qodo Merge, Sourcery, and PR-Agent. Pricing, accuracy, and GitHub integration.
AI Coding Tools Comparison 2026: GitHub Copilot vs Cursor vs Windsurf vs Cline
Comprehensive comparison of the best AI coding tools in 2026: GitHub Copilot, Cursor, Windsurf, and Cline. Pricing, features, model support, and real-world benchmarks.
AI Pair Programming in 2026: GitHub Copilot vs Cursor vs Claude Code
Master AI pair programming in 2026. Compare GitHub Copilot, Cursor, and Claude Code for autocomplete, chat, and agentic workflows. Includes setup guides and productivity tips.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →Why Multi-Stage Builds Matter
A single-stage Dockerfile for a Node.js app installs all dev dependencies, build tools, and generates artifacts in one layer. The final image contains everything — webpack, TypeScript compiler, test frameworks — that your production server never needs.
| Approach | Node.js App | Go Binary | Python Service |
|---|---|---|---|
| Single-stage (full image) | ~1.4 GB | ~1.2 GB | ~3.5 GB |
| Multi-stage (alpine) | ~120 MB | ~15 MB | ~400 MB |
| Multi-stage (distroless) | ~100 MB | ~5 MB | ~200 MB |
Pattern 1: Node.js Builder + Nginx Runner
# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
# Cache dependency install separately from source code
COPY package.json package-lock.json ./
RUN npm ci --prefer-offline
# Now copy source and build
COPY . .
RUN npm run build
# Stage 2: Production (only nginx + built assets)
FROM nginx:1.27-alpine AS production
# Copy built artifacts from builder stage — nothing else
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
# Result: ~25MB vs ~1.4GB single-stage Pattern 2: Go Static Binary
# Stage 1: Build with full Go toolchain
FROM golang:1.22-alpine AS builder
WORKDIR /build
# Cache module download
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# CGO_ENABLED=0 produces a fully static binary (no libc dependency)
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o server .
# Stage 2: Scratch — literally nothing (0 bytes base)
FROM scratch AS production
COPY --from=builder /build/server /server
# Copy CA certs for HTTPS requests
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
EXPOSE 8080
ENTRYPOINT ["/server"]
# Result: ~7MB vs ~1.2GB single-stage Pattern 3: Python with BuildKit Cache Mounts
# syntax=docker/dockerfile:1
FROM python:3.12-slim AS builder
WORKDIR /app
# BuildKit cache mount: pip cache persists between builds
# (not stored in image layers)
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --prefix=/install -r requirements.txt
# Stage 2: Minimal runtime
FROM python:3.12-slim AS production
WORKDIR /app
# Copy only installed packages
COPY --from=builder /install /usr/local
COPY src/ ./src/
# Non-root user for security
RUN useradd -m appuser
USER appuser
CMD ["python", "-m", "src.main"] Build Cache Optimization Order
Layer order determines cache effectiveness. Put the slowest, least-changing operations first:
Bad (cache invalidated on any source change)
COPY . . # All source files
RUN npm ci # Reinstalls every time! Good (dependencies cached separately)
COPY package*.json . # Only manifest
RUN npm ci # Cached until manifest changes
COPY . . # Source changes don't bust dep cache Using --target for Development Builds
# Build specific stages
docker build --target builder -t myapp:dev . # Dev image with all tools
docker build --target production -t myapp:prod . # Lean production image
# Multi-stage for CI: run tests in builder, build prod separately
docker build --target test -t myapp:test .
docker run --rm myapp:test npm test
# Only build prod if tests pass
docker build --target production -t myapp:latest .