dockerfiles
Skill8 starsChanged 9 days agoMITView on GitHub
Writes and reviews production-grade Dockerfiles and container images, covering multi-stage builds, pinned minimal base images, non-root users, cache-friendly layers, secret-free builds, signal handling and health checks. Use when creating or editing a Dockerfile, .dockerignore or compose file, shrinking or hardening an image, fixing slow container builds, or containerizing an app. Not for Kubernetes manifests.
- Pipes a download into a shell
- Reads credentials
- Deletes or force-pushes
- Installs packages
In this repository60 other files it gives its agents.
- api-designSkill
- auth-implementationSkill
- background-jobsSkill
- building-agentsSkill
- caching-strategiesSkill
- ci-cd-pipelinesSkill
- cloud-cost-optimizationSkill
- codebase-onboardingSkill
- code-review-changesSkill
- concurrency-patternsSkill
- database-migrationsSkill
- database-schema-designSkill
- database-testingSkill
- data-pipelinesSkill
- data-privacy-retentionSkill
- dependency-upgradesSkill
- design-docsSkill
- design-systemsSkill
- domain-driven-designSkill
- event-driven-architectureSkill
- file-storage-uploadsSkill
- frontend-securitySkill
- git-workflowSkill
- graphql-apisSkill
- grpc-servicesSkill
- incident-responseSkill
- infrastructure-as-codeSkill
- internationalizationSkill
- kubernetes-deploymentsSkill
- large-scale-migrationsSkill
- legacy-refactoringSkill
- llm-evalsSkill
- llm-integrationSkill
- mcp-serversSkill
- micro-frontendsSkill
- ml-experimentsSkill
- monorepo-managementSkill
- nosql-designSkill
- observabilitySkill
- orm-patternsSkill
- performance-profilingSkill
- postgres-operationsSkill
- rag-systemsSkill
- react-patternsSkill
- realtime-apisSkill
- resilience-patternsSkill
- root-cause-debuggingSkill
- search-integrationSkill
- security-auditSkill
- seo-and-ssrSkill
- sql-optimizationSkill
- system-designSkill
- test-strategySkill
- threat-modelingSkill
- typescript-patternsSkill
- unit-testingSkill
- verify-before-doneSkill
- web-accessibilitySkill
- web-performanceSkill
- writing-skillsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
17 rules. About 0 are common, 4 are distinctive, and 13 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
13
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 18
Not for: Kubernetes manifests, Helm charts, or cluster configuration.
- Very specific: line 26
Add
.dockerignore: before the first build. - Very specific: line 57
Never use latest. Use a specific version tag, and for production pin the digest too:
- Very specific: line 76
Use BuildKit cache mounts for package manager caches (add # syntax=docker/dockerfile:1 as the first line):
- Distinctive: line 96
Keep the build context small, and keep secrets out of it:
- Very specific: line 123
Never pass secrets with ARG, ENV or COPY.
- Very specific: line 124
Use BuildKit secret mounts, which exist only for that one RUN:
- Very specific: line 144
Use a numeric UID in USER, so Kubernetes runAsNonRoot can verify it.
- Distinctive: line 146
Leave application files owned by root and readable.
- Distinctive: line 151
When the platform stops a container it sends SIGTERM, waits for a grace period (10 seconds by default in Docker), then sends SIGKILL.
- Very specific: line 153
Use exec form: CMD ["node", "server.js"].
- Very specific: line 154
Do not use npm start or other wrapper scripts as PID 1.
- Very specific: line 159
Test it: docker stop <container> should return well before the grace period ends.
- Very specific: line 174
Use COPY, not ADD, unless you specifically need ADD's archive extraction.
- Very specific: line 175
Use WORKDIR, not RUN cd ....
- Very specific: line 177
Add OCI labels for traceability: LABEL org.opencontainers.image.source="https://github.com/org/repo".
- Very specific: line 179
Do not curl | sh installers in the Dockerfile.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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
Dockerfiles
A production image contains only what is needed to run the app: no compilers, no secrets, no root user. It builds reproducibly from pinned inputs, and it shuts down cleanly when asked.
When to use
- Containerizing an application, or writing a new Dockerfile.
- Reviewing or hardening an existing Dockerfile.
- Builds are slow, images are large, or scanners report many vulnerabilities.
- Writing
.dockerignoreor acompose.yamlfor local development.
Not for: Kubernetes manifests, Helm charts, or cluster configuration.
Workflow
- Learn the app: language and version (from
.nvmrc,go.mod,pom.xml,pyproject.toml,*.csproj), build command, the artifact that runs (binary, jar,dist/), port, config via environment variables, and writable paths. - Choose the base images for the build stage and the runtime stage (table below).
- Write a multi-stage Dockerfile: dependencies → build → minimal runtime. Start from references/examples.md for Node, Python, Go, Java and .NET.
- Order layers for caching: copy only the lockfiles, install dependencies, then copy the source.
- Add
.dockerignorebefore the first build. - Harden: non-root numeric user, no secrets in any layer, pinned base images, no build tools in the final stage.
- Make it stop cleanly: exec-form
CMD/ENTRYPOINT, and an init process or proper SIGTERM handling. Build, run and inspect:
docker build -t app:dev . docker run --rm -p 8080:8080 app:dev docker image ls app:dev # size docker history app:dev # what each layer added- Lint and scan:
hadolint Dockerfile(ordocker run --rm -i hadolint/hadolint < Dockerfile), thentrivy image app:devordocker scout cves app:dev. Fix findings by updating the base image or dependencies, not by suppressing them. - Do not push images to a registry, or change tags others deploy from, without explicit confirmation from the user.
Base images
| App | Build stage | Runtime stage |
| Go, Rust (static binary) | golang:<ver>, rust:<ver> | gcr.io/distroless/static-debian12:nonroot, or scratch (then add CA certificates and time zone data yourself) |
| Node.js | node:<ver>-slim | node:<ver>-slim, or gcr.io/distroless/nodejs<major>-debian12 |
| Python | python:<ver>-slim | python:<ver>-slim |
| Java | maven:<ver>-eclipse-temurin-<jdk> or gradle:<ver>-jdk<jdk> | eclipse-temurin:<jdk>-jre, or gcr.io/distroless/java<jdk>-debian12 |
| .NET | mcr.microsoft.com/dotnet/sdk:<ver> | mcr.microsoft.com/dotnet/aspnet:<ver> (or runtime:<ver>); chiseled variants are smaller |
- Match the version to the project's runtime version. Check that the tag exists before using it (
docker buildx imagetools inspect <image:tag>). - Alpine uses musl libc. Python wheels and Node native modules often need compiling or behave differently on it. Prefer Debian
-slimunless you have tested Alpine. - Distroless and chiseled images have no shell or package manager. They are smaller and safer, but you cannot
docker execinto them with a shell, andHEALTHCHECKcommands that callcurlwill not work.
Pin base images
Never use
latest. Use a specific version tag, and for production pin the digest too:FROM node:22-slim@sha256:<digest>- Get the digest with
docker buildx imagetools inspect node:22-slim(theDigest:line). Never invent a digest. - Let Renovate or Dependabot update tags and digests, so pinning does not mean never patching.
Layer caching
COPY package.json package-lock.json ./
RUN npm ci # re-runs only when the lockfile changes
COPY . . # source changes invalidate only from here on
RUN npm run build- Copy only what each step needs. A
COPY . .before installing dependencies reinstalls everything on every code change. Use BuildKit cache mounts for package manager caches (add
# syntax=docker/dockerfile:1as the first line):Ecosystem Cache mount npm RUN --mount=type=cache,target=/root/.npm npm cipip RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txtGo RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build go build ...Maven RUN --mount=type=cache,target=/root/.m2 mvn -B packageNuGet RUN --mount=type=cache,target=/root/.nuget/packages dotnet restoreCombine
apt-get updateandinstallin oneRUN, and clean up in the same layer:RUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates \ && rm -rf /var/lib/apt/lists/*
.dockerignore
Keep the build context small, and keep secrets out of it:
.git
**/node_modules
**/dist
**/build
**/target
**/bin
**/obj
**/.venv
**/__pycache__
coverage
*.log
.env
.env.*
**/*.pem
**/*.key
Dockerfile*
compose*.yaml
docker-compose*.ymlAdjust it to the project: never exclude files the build needs.
Secrets
- Never pass secrets with
ARG,ENVorCOPY. They end up in the image metadata or layers, anddocker historyshows build arguments. Use BuildKit secret mounts, which exist only for that one
RUN:RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm cidocker build --secret id=npmrc,src=$HOME/.npmrc -t app:dev .- Runtime secrets come from the platform (environment variables or mounted files from a secret manager), never baked into the image.
Non-root user
# Debian/Ubuntu-based images
RUN useradd --system --uid 10001 --no-create-home app
USER 10001- Use a numeric UID in
USER, so KubernetesrunAsNonRootcan verify it. - Many images already ship a user:
nodein official Node images,nonroot(UID 65532) in distroless:nonroottags, and$APP_UIDin .NET 8+ images. - Leave application files owned by root and readable. The app should not be able to modify its own code. Give write access only to the directories that need it.
- Listen on a port above 1024 (8080, 3000), because non-root users cannot bind to low ports by default.
Signals and PID 1
When the platform stops a container it sends SIGTERM, waits for a grace period (10 seconds by default in Docker), then sends SIGKILL.
- Use exec form:
CMD ["node", "server.js"]. Shell form (CMD node server.js) runs under/bin/sh -c, which does not forward signals to your app. - Do not use
npm startor other wrapper scripts as PID 1. If you need an entrypoint script, end it withexec "$@"so the app replaces the shell. - A process running as PID 1 gets no default signal handlers, so the app must handle SIGTERM itself, or run under an init process:
docker run --init, orinit: truein compose.- For Kubernetes and other platforms without
--init, install tini in the image (Debian:apt-get install -y --no-install-recommends tini) and useENTRYPOINT ["/usr/bin/tini", "--"].
- On SIGTERM the app should stop accepting new work, finish in-flight requests, close connections, and exit.
- Test it:
docker stop <container>should return well before the grace period ends.
Health checks
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
CMD ["curl", "-fsS", "http://localhost:8080/healthz"]- Docker and compose use
HEALTHCHECK. Kubernetes ignores it and uses liveness and readiness probes instead. - The command must exist in the image. Distroless images have no
curl; use a tiny health-check binary or rely on the orchestrator's probes.
Other rules
- One main process per container. Use separate containers (or compose services) for the web server, worker and database, not supervisord.
- Use
COPY, notADD, unless you specifically needADD's archive extraction. - Use
WORKDIR, notRUN cd .... - Configure via environment variables. Do not bake environment-specific config into the image: build once, run in every environment.
- Add OCI labels for traceability:
LABEL org.opencontainers.image.source="https://github.com/org/repo". - Multi-architecture builds:
docker buildx build --platform linux/amd64,linux/arm64 -t <image> .. - Do not
curl | shinstallers in the Dockerfile. Download a pinned version and verify its checksum.
Compose for local development
# compose.yaml (the top-level "version:" key is obsolete; omit it)
services:
app:
build: .
ports: ["8080:8080"]
env_file: .env
init: true
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: dev-only-password
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 10
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:Credentials in a compose file are for local development only. Never reuse them anywhere else.
Anti-patterns
FROM something:latest: builds change under you, and rollbacks are not reproducible.- Running as root.
- Secrets in
ARG,ENV, copied files, or.npmrc/pip.confleft in a layer. COPY . .before dependency installation.- No
.dockerignore, so.git,node_modulesand.envend up in the context or the image. - Compilers and build tools in the runtime image (use multi-stage).
- Shell-form
CMD, ornpm startas PID 1, so the container ignores SIGTERM and is killed after the grace period. apt-get updatein its own layer (stale cache), or no cleanup of/var/lib/apt/lists.- Writing application data into the container filesystem instead of a volume or external storage.
Done when
- The image builds from pinned base images, and rebuilds without source changes are fully cached.
- The final stage contains no compilers, package caches,
.git, tests or secrets (docker historyand a scan confirm it). - The container runs as a non-root numeric UID.
docker stopstops it promptly, and the app handles SIGTERM gracefully.hadolintpasses, or has only justified ignores, and the scan shows no fixable critical or high vulnerabilities..dockerignoreexists, and image size is recorded before and after the change.
References
- references/examples.md — read when writing a Dockerfile for Node.js, Python, Go, Java or .NET; contains complete multi-stage examples.
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.

