dockerfile-best-practices
Skill5 starsChanged 4 months agoMITView on GitHub
Write or review a Dockerfile and compose setup for a production service: multi-stage builds that ship only the runtime, pinned base images, layer order for cache hits, non-root user, minimal image size, correct signal handling and health checks, secrets kept out of layers, and language-specific patterns for Node, Python, Go, and Java. Use when containerising a service, when images are large or slow to build, when a container ignores SIGTERM, or when a security scan flags the image.
- Reads credentials
- Deletes or force-pushes
- Installs packages
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 1 is distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 41
Prefer -slim or distroless over alpine for glibc-dependent runtimes (Node native modules, Python wheels).
- Very specific: line 52
No secrets in ENV, ARG, or copied files.
- Very specific: line 53
Read-only root filesystem where possible (--read-only with tmpfs for /tmp).
- Very specific: line 64
Check with docker image ls and dive.
- Distinctive: line 65
Do not install dev dependencies, docs, or test files in the runtime stage.
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 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
A production image contains exactly what the process needs to run, built reproducibly, running as a non-root user, and stopping cleanly when asked. Everything below follows from those four properties.
Structure: multi-stage
# syntax=docker/dockerfile:1.7
FROM node:22.11-bookworm-slim AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable && pnpm install --frozen-lockfile --prod=false
FROM deps AS build
COPY . .
RUN pnpm build && pnpm prune --prod
FROM node:22.11-bookworm-slim AS runtime
ENV NODE_ENV=production
WORKDIR /app
RUN groupadd -r app && useradd -r -g app -d /app app
COPY --from=build --chown=app:app /app/node_modules ./node_modules
COPY --from=build --chown=app:app /app/dist ./dist
COPY --chown=app:app package.json ./
USER app
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s CMD node dist/healthcheck.js
ENTRYPOINT ["node", "dist/server.js"]Build stages hold compilers and dev dependencies; the runtime stage copies artifacts only.
Rules
Base images
- Pin to a specific version tag (
22.11-bookworm-slim), and for reproducible builds pin the digest (@sha256:...) with a tool (Renovate, Dependabot) to bump it. - Prefer
-slimor distroless overalpinefor glibc-dependent runtimes (Node native modules, Python wheels); alpine for Go static binaries is fine. - One process per container; no supervisord.
Layers and cache
- Order from least to most frequently changed: base, system packages, dependency manifests, dependency install, source, build. Copy lockfiles before source so dependency layers cache.
RUNsteps combined with&&and cleanup in the same step (apt-get install ... && rm -rf /var/lib/apt/lists/*); a laterrmdoes not shrink an earlier layer..dockerignorewith.git,node_modules, build output,.env*, tests, docs; without it the build context and the cache invalidation are both wrong.- BuildKit cache mounts for package managers:
RUN --mount=type=cache,target=/root/.npm npm ci.
Security
USERnon-root beforeENTRYPOINT; files copied with--chown. Bind to a port above 1024.- No secrets in
ENV,ARG, or copied files; they persist in layers and history. UseRUN --mount=type=secret,id=npmrcfor build-time secrets and runtime injection (env at run, mounted files, a secrets manager) for the rest. - Read-only root filesystem where possible (
--read-onlywithtmpfsfor/tmp); drop capabilities; no--privileged. - Scan (
docker scout,trivy image) in CI; fail on critical CVEs with a fix available.
Runtime behaviour
ENTRYPOINTin exec form (JSON array) so the process is PID 1 and receives signals;CMDfor default arguments only. Shell form wraps insh -cand swallows SIGTERM.- The application handles SIGTERM: stop accepting, drain in-flight requests, close connections, exit within the orchestrator's grace period (default 30 s). If the runtime cannot (some shell scripts), use
tinias the init (ENTRYPOINT ["tini", "--", ...]). HEALTHCHECK(or the orchestrator's probe) hits a cheap endpoint that checks dependencies the process needs; readiness separate from liveness in Kubernetes.- Logs to stdout/stderr, unbuffered (
PYTHONUNBUFFERED=1); no log files in the container. EXPOSEdocuments the port; it does not publish it.
Size
- Check with
docker image lsanddive. Typical targets: Go static under 20 MB (distroless/static), Node under 200 MB, Python under 250 MB. - Do not install dev dependencies, docs, or test files in the runtime stage.
Language notes
- Node:
npm ci/pnpm install --frozen-lockfile;NODE_ENV=production;nodedirectly, notnpm start(npm does not forward signals); set--max-old-space-sizefrom the container limit or use a version that respects cgroups. - Python:
python:3.12-slim;pip install --no-cache-dir -r requirements.txt(oruv sync --frozen) in a build stage into a venv copied to runtime;PYTHONDONTWRITEBYTECODE=1; run withgunicorn/uvicorndirectly;--workersfrom CPU limit. - Go: build stage
golang:1.23,CGO_ENABLED=0 go build -ldflags="-s -w", runtimegcr.io/distroless/static-debian12:nonroot, copy the binary and CA certs. - Java: build with the JDK, run on a JRE (
eclipse-temurin:21-jre), layered jar (spring-boot:build-imageorjarmode=layertools),-XX:MaxRAMPercentage=75.
Compose for local development
compose.yaml with the service built from the Dockerfile's build stage (target: build), source bind-mounted for hot reload, dependencies (db, cache) with named volumes and health checks, depends_on with condition: service_healthy, env from .env (git-ignored) with an .env.example committed.
Review checklist
Pinned base; multi-stage; lockfile copied before source; .dockerignore; non-root; exec-form ENTRYPOINT; signals handled; health check; no secrets in layers (docker history shows none); scan clean; size within target; builds reproducibly twice with the same digest.
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- obeone/claude-skills★ 4same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository89 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- site/llms-full.txtllms.txt
- site/llms.txtllms.txt
- improveSkill
- academic-writingSkill
- benchmark-reportingSkill
- bibtex-managerSkill
- citation-graphSkill
- citation-verifierSkill
- design-science-methodologySkill
- docx-thesisSkill
- experiment-reproducibilitySkill
- latex-toolkitSkill
- literature-searchSkill
- lit-review-synthesisSkill
- paper-readerSkill
- proofread-academicSkill
- research-figuresSkill
- research-methodologySkill
- research-questionSkill
- statistics-advisorSkill
- supervisor-updateSkill
- systematic-reviewSkill
- thesis-proposalSkill
- coolify-image-appSkill
- coolify-vps-baselineSkill
- deploy-verificationSkill
- express-prisma-coolify-imageSkill
- ghcr-coolify-pipelineSkill
- live-domain-cutoverSkill
- nextjs-coolify-imageSkill
- proxy-client-addressSkill
- vps-ops-toolkitSkill
- adr-writingSkill
- openapi-specSkill
- readme-writingSkill
- technical-writingSkill
- adversarial-reviewerSkill
- api-design-reviewSkill
- code-review-responseSkill
- create-agentSkill
- create-commandSkill
- create-hookSkill
- create-skillSkill
- db-migration-safetySkill
- dependency-upgradeSkill
- error-handling-patternsSkill
- executing-plansSkill
- explain-codebaseSkill
- fix-ciSkill
- github-actions-pipelineSkill
- grill-with-docsSkill
- incident-postmortemSkill
- logging-and-observabilitySkill
- parallel-agentsSkill
- performance-optimizationSkill
- refactoring-recipesSkill
- solid-principlesSkill
- systematic-debuggingSkill
- test-driven-developmentSkill
- The first 60 shown.
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.

