docker-multi-stage
Skill4 starsChanged 5 months agoMITView on GitHub
Creates optimized multi-stage Dockerfiles for any language or framework. Use when building, reviewing, or refactoring Dockerfiles, container images, or Docker Compose configurations. Covers stage structure, base image selection, layer caching, security hardening, and healthchecks.
- Reads credentials
- Deletes or force-pushes
- Installs packages
In this repository28 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- appkit-commonsSkill
- boostSkill
- code-cleanupSkill
- commit-msgSkill
- create-readmeSkill
- frontend-designSkill
- github-actions-cicdSkill
- python-clean-codeSkill
- python-codingSkill
- reflex-docsSkill
- reflex-state-and-architectureSkill
- reflex-testing-stateSkill
- releaseSkill
- runic-migrateSkill
- runic-ogmSkill
- terraform-azure-verified-modulesSkill
- terraform-refactor-moduleSkill
- terraform-run-acceptance-testsSkill
- terraform-search-importSkill
- terraform-stacksSkill
- terraform-style-guideSkill
- terraform-testSkill
- appkit-commonsSkill
- appkit-mantine-referenceSkill
- appkit-userSkill
The file
We split this skill into the rules it gives an agent
12 rules. About 0 are common, 2 are distinctive, and 10 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
10
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 31
Use meaningful stage names with the AS keyword.
- Very specific: line 48
Never install build-only tools (gcc, make, node-gyp) in the runtime stage.
- Very specific: line 58
Always pin to a specific tag: python:3.13-slim-bookworm, node:22-alpine3.20.
- Distinctive: line 84
Use BuildKit cache mounts to persist package manager caches across builds:
- Very specific: line 129
Never COPY .env files or secret files into the image.
- Very specific: line 130
Use --mount=type=secret for build-time secrets:
- Distinctive: line 136
Use runtime env vars or secret managers (Vault, Key Vault) at container start.
- Very specific: line 140
Remove package caches: rm -rf /var/lib/apt/lists/*
- Very specific: line 141
Avoid installing sudo, ssh, vim in runtime images.
- Very specific: line 170
Use ARG for values that vary between build environments.
- Very specific: line 171
Use ENV for runtime configuration with sensible defaults.
- Very specific: line 176
Always create a .dockerignore to exclude unnecessary files:
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Multi-Stage Dockerfiles
Quick reference
- Multi-stage builds — separate builder from runtime; copy only artifacts.
- Pin versions — use exact image tags (
python:3.13-slim-bookworm, notpython). - Minimize layers — combine
RUNcommands with&&; order from least→most changing. - Non-root user — always set
USERin the final stage. - Cache mounts — use
--mount=type=cachefor package manager caches. - Healthchecks — add
HEALTHCHECKfor production readiness.
Stage structure
dependencies → build → (test) → runtimeUse meaningful stage names with the AS keyword.
# ── Stage 1: Builder ──
FROM python:3.13-slim-bookworm AS builder
# install build deps, compile, fetch packages
# ── Stage 2: Runtime ──
FROM python:3.13-slim-bookworm AS runtime
# copy only runtime artifacts from builder
COPY --from=builder /app /appKey rules
- Builder stage — install compilers, dev headers, build tools. Run
pip install,npm ci,cargo build, etc. - Runtime stage — start from a minimal base; copy only the built output, virtual env, or binary.
- Never install build-only tools (
gcc,make,node-gyp) in the runtime stage.
Base image selection
| Goal | Recommended base | Notes |
| Smallest possible | distroless / alpine | No shell; harder to debug |
| Balance size + compat | *-slim variants | Good default for Python, Node |
| Full tooling needed | *-bookworm / *-bullseye | Use only in builder stage |
- Always pin to a specific tag:
python:3.13-slim-bookworm,node:22-alpine3.20. - Match builder and runtime base OS family when possible to avoid glibc mismatches.
Layer optimization
Order: stable → volatile
# 1. System deps (rarely change)
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# 2. Dependency manifests (change occasionally)
COPY pyproject.toml uv.lock ./
# 3. Install deps (cached unless manifests change)
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-install-project
# 4. Application code (changes frequently)
COPY . .Cache mounts
Use BuildKit cache mounts to persist package manager caches across builds:
# Python (uv / pip)
RUN --mount=type=cache,target=/root/.cache/uv uv sync --frozen
# Node.js (npm)
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
# Go
RUN --mount=type=cache,target=/root/.cache/go-build go build -o /appCombine RUN commands
# ✅ Single layer — fewer layers, smaller image
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# ❌ Multiple layers — bloats image
RUN apt-get update
RUN apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*Security practices
Non-root user
# Create a non-root user in the runtime stage
RUN groupadd --gid 1000 appuser \
&& useradd --uid 1000 --gid appuser --shell /bin/false appuser
# Set ownership
COPY --from=builder --chown=appuser:appuser /app /app
WORKDIR /app
USER appuserSecrets handling
- Never
COPY.envfiles or secret files into the image. - Use
--mount=type=secretfor build-time secrets:
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci- Use runtime env vars or secret managers (Vault, Key Vault) at container start.
Minimal attack surface
- Remove package caches:
rm -rf /var/lib/apt/lists/* - Avoid installing
sudo,ssh,vimin runtime images. - Set
ENV PYTHONDONTWRITEBYTECODE=1to skip.pycfiles. - Set
ENV PYTHONUNBUFFERED=1for proper log streaming.
Healthchecks
# HTTP-based
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:${PORT}/health || exit 1
# TCP-based (when curl is unavailable)
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:${PORT}/health')"Build arguments & env vars
# Build-time configuration
ARG PYTHON_VERSION=3.13
ARG APP_ENV=production
# Runtime environment
ENV NODE_ENV=production \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1- Use
ARGfor values that vary between build environments. - Use
ENVfor runtime configuration with sensible defaults. ARGvalues are not persisted in the final image (security-safe).
.dockerignore
Always create a .dockerignore to exclude unnecessary files:
.git
.github
.venv
__pycache__
*.pyc
node_modules
.env
.env.*
*.md
tests/
htmlcov/
.mypy_cache/
.ruff_cache/
.pytest_cache/Complete example (Python / uv)
# ── Stage 1: Builder ──
ARG PYTHON_VERSION=3.13-slim-bookworm
FROM python:${PYTHON_VERSION} AS builder
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& rm -rf /var/lib/apt/lists/*
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock .python-version ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-install-project --all-extras
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --all-extras
# ── Stage 2: Runtime ──
FROM python:${PYTHON_VERSION} AS runtime
ARG PORT=8000
ENV PORT=${PORT} \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
RUN groupadd --gid 1000 appuser \
&& useradd --uid 1000 --gid appuser --shell /bin/false appuser
WORKDIR /app
COPY --from=builder --chown=appuser:appuser /app /app
USER appuser
EXPOSE ${PORT}
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:${PORT}/health')"
CMD ["uv", "run", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "${PORT}"]Decision tree
- Need build tools (gcc, make)? → Put them in the builder stage only.
- Python project? → Use
uv syncwith cache mounts; copy.venvto runtime. - Node.js project? →
npm ci --omit=devin builder; copynode_modulesto runtime. - Go / Rust? → Build static binary in builder; use
distrolessorscratchfor runtime. - Need shell access for debugging? → Use
*-slimoverdistroless. - Multi-arch build? → Use
docker buildx build --platform linux/amd64,linux/arm64.
Pre-commit checklist
- Multi-stage build separates builder from runtime
- Base image tags are pinned to specific versions
- Layers ordered from stable to volatile for cache efficiency
- Cache mounts used for package managers
- Non-root user in the final stage
- No secrets baked into the image
.dockerignoreexcludes unnecessary filesHEALTHCHECKdefined for production imagesEXPOSEmatches the actual application port- Image scanned for vulnerabilities (
docker scout,trivy)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

