Use it when writing or reviewing a Dockerfile.
Dockerfile Best Practices
Dockerfile best practices: small trusted base images, multi-stage builds, pinned versions, combined RUN steps that clean caches, a non-root USER, COPY instead of ADD, a .dockerignore, and an explicit HEALTHCHECK/ENTRYPOINT.
1. Lint: hadolint Dockerfile.
2. Base image pinned and minimal?
3. Multi-stage to drop build tools?
4. USER non-root set?
5. Build and scan: docker build then trivy image.
Dockerfile → build context → layers → image
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Build twice and confirm the second build reuses cache. Inspect the final image for build tools (for example gcc) that should not be there. Run the container and confirm it handles SIGTERM by stopping cleanly within the grace period.
Step 2 Common failures Narrow the fault
Shell-form ENTRYPOINT runs the app under /bin/sh, which does not forward SIGTERM, so stops take the full timeout. Using latest as the base tag makes builds unreproducible. apt-get update in a separate layer from install leads to stale package lists.
Never bake secrets into images — use build secrets. Order layers for caching. Use exec-form ENTRYPOINT so signals reach the process.
hadolint Dockerfile— Dockerfile lintdocker ps -a— containers and exit codesdocker logs <c>— container outputdocker inspect <c>— config, mounts, network, state
ReferenceWhat it is1 section · depth on demand
How it works
Each instruction produces a cache key from its inputs. When COPY . . comes before dependency installation, any source change invalidates the dependency layer. A .dockerignore keeps build context small and avoids copying secrets or .git into the image. HEALTHCHECK and an exec-form ENTRYPOINT make signals and health work correctly.
ReferenceImportant terms & boundaries2 sections · depth on demand
Common mistakes
- Not using multi-stage builds.
In depth
A good Dockerfile is fast to rebuild, small, reproducible and runs as a well-behaved process.
Multi-stage build — compile in one stage, ship only the result
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download # cached until dependencies change
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static:nonroot
COPY --from=build /out/app /app
USER nonroot
ENTRYPOINT ["/app"]
Rules that matter most
| Rule | Why |
|---|---|
| Copy dependency manifests before source | Source edits do not invalidate the dependency layer |
| Pin the base image by version (or digest) | latest changes under you and breaks reproducibility |
One RUN for install + cleanup | Deleted files in a later layer still ship |
Exec-form ENTRYPOINT ["app"] | The app is PID 1 and receives SIGTERM directly |
.dockerignore with .git, node_modules, secrets | Smaller build context, no leaked files |
USER with a numeric UID | Non-root, and works with runAsNonRoot checks |
Build secrets via --mount=type=secret | Secrets never land in a layer or in history |
Signal handling
With shell form (ENTRYPOINT app --flag) PID 1 is /bin/sh, which does not forward SIGTERM. docker stop then waits the full grace period and sends SIGKILL. If the app cannot handle being PID 1 (reaping zombies), run it with --init or a minimal init such as tini.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.