Pcornel
Browse topics
Options
Recent
Favorites
Containers · Technical reference

Dockerfile Best Practices

Direct answer

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.

When to use this

Use it when writing or reviewing a Dockerfile.

First 30 seconds

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.

Operational verification

Dockerfile → build context → layers → image

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Docker / containerd / Podman / Kubernetes / OCI-compatible platforms
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformDocker / containerd / Podman / Kubernetes / OCI-compatible platforms
Scope / prerequisiteUse it when writing or reviewing a Dockerfile.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Never bake secrets into images — use build secrets. Order layers for caching. Use exec-form ENTRYPOINT so signals reach the process.

Key checks
  • hadolint Dockerfile — Dockerfile lint
  • docker ps -a — containers and exit codes
  • docker logs <c> — container output
  • docker 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

RuleWhy
Copy dependency manifests before sourceSource 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 + cleanupDeleted 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, secretsSmaller build context, no leaked files
USER with a numeric UIDNon-root, and works with runAsNonRoot checks
Build secrets via --mount=type=secretSecrets 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

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.

Aliases that resolve here
dockerfile best practicesdockerfile multi-stage builddocker build cache
Confirmation

Confirm action

Continue?