Skip to main content
Foundations: web, servers, containersLesson 11 of 13

Writing a Good Dockerfile

A Dockerfile that builds is not yet a good one. Follow one image from source tree to a running Pod and see which lines make it faster to build, smaller, safer and easier to stop.

Rebuilt on editeverything
Final imageOS + toolchain + app
Runs asroot
PID 1sh -c
docker build (BuildKit)Source treecode, lockfile, .git.dockerignorefilters the contextBuild secrettoken, never a layerBuild contextfiles sent to builderBuild stagecompiler, deps, testsBase imageslim or distrolessImage layerscached, reusedRuntime stageapp binary onlyPodcontainer, PID 1kubeletprobes, SIGTERMCOPY . .
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Order lines so a code change does not reinstall dependencies
  • Multi-stage builds keep the toolchain out of the final image
  • Why slim or distroless bases and a non-root USER
  • Exec-form ENTRYPOINT so SIGTERM reaches your process
  • Build secrets, reproducible builds, and probes instead of HEALTHCHECK

Line by line: common mistake and better habit​

LineCommon mistakeBetterWhy
COPYCOPY . . before installing dependenciesCopy lockfiles, install, then copy sourceA code edit no longer reinstalls everything
.dockerignoreMissingExclude .git, dependency folders, build output, .envSmaller context, stable cache, no leaked files
FROMOne stage on a full SDK imageBuild stage plus a slim or distroless runtime stageToolchain and source stay out of the image
FROMFloating tag like node:22image:tag@sha256:..., updated by a botSame commit builds the same image
USERNone (root)Numeric non-root UIDWorks with runAsNonRoot, smaller blast radius
CMD / ENTRYPOINTShell form CMD node server.jsExec form CMD ["node", "server.js"]SIGTERM reaches the app, rollouts stop cleanly
ARG / ENV / COPYToken passed or copied inRUN --mount=type=secretSecret never lands in a layer or metadata
HEALTHCHECKRelied on in KubernetesReadiness, liveness and startup probesThe kubelet does not run HEALTHCHECK

Try it yourself​

docker build -t my-app:dev . # uses BuildKit by default
docker history my-app:dev # every layer, its size and the line that made it
docker image inspect my-app:dev \
--format 'user={{.Config.User}} entrypoint={{json .Config.Entrypoint}} cmd={{json .Config.Cmd}}'
docker buildx imagetools inspect gcr.io/distroless/static-debian12:nonroot # digest to pin in FROM
docker build --secret id=npmrc,src=$HOME/.npmrc -t my-app:dev . # pair with RUN --mount=type=secret,id=npmrc
docker build --build-arg SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) -t my-app:dev . # fixed timestamps
kubectl debug -it pod/my-app --image=busybox:1.36 --target=app # shell next to a distroless container

Check yourself

  1. A Dockerfile does COPY . . and then RUN npm ci. Why does every small code edit take as long as a clean build?
  2. A Pod with CMD node server.js (shell form) always takes the full grace period to stop. What is happening?
  3. A build copies .npmrc, runs npm ci, then deletes .npmrc in a later RUN. Is the token gone from the image?

Further reading: Docker build best practices · Multi-stage builds · Build secrets · Configure probes · How an Image Is Built and Run · Image and Supply Chain Security · Health checks — alive, and ready for requests?