claude-home/docker/examples/multi-stage-builds.md
Cal Corum 4b7eca8a46
All checks were successful
Reindex Knowledge Base / reindex (push) Successful in 3s
docs: add YAML frontmatter to all 151 markdown files
Adds title, description, type, domain, and tags frontmatter to every
doc for improved KB semantic search. The description field is prepended
to every search chunk, and domain/type/tags enable filtered queries.

Type values: context, guide, runbook, reference, troubleshooting
Domain values match directory structure (networking, docker, etc.)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 09:00:44 -05:00

1.4 KiB

title description type domain tags
Multi-Stage Docker Builds Reference examples for multi-stage Docker builds in Node.js and Python, showing how to minimize image size and separate build from runtime dependencies. reference docker
docker
dockerfile
multi-stage
nodejs
python
optimization

Multi-Stage Docker Builds

Basic Multi-Stage Pattern

# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

# Production stage
FROM node:18-alpine AS production
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Python with Virtual Environment

# Build stage
FROM python:3.11-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir -r requirements.txt

# Production stage
FROM python:3.11-slim AS production
WORKDIR /app
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY . .
CMD ["python", "app.py"]

Benefits

  • Smaller final image size
  • Separation of build and runtime dependencies
  • Better caching strategies
  • Security (no build tools in production image)

Best Practices

  • Use specific base image versions
  • Copy only necessary files to production stage
  • Run as non-root user in production stage
  • Use .dockerignore to exclude unnecessary files