It builds, but every edit takes minutes The first Dockerfile is often four lines: FROM a full OS image, COPY . ., RUN install, CMD. It works. But each code edit reinstalls every dependency, the image ships a compiler, and it runs as root.
How layers and tags work is covered in the image lesson. Here we fix the recipe itself, one line at a time.
Copy the dependency list first, then the source Each instruction makes a layer, and a changed layer invalidates every layer after it. If COPY . . comes before the install step, any code edit forces a full reinstall.
Copy only the manifests first (package.json and its lockfile, go.mod and go.sum, requirements.txt), run the install, and copy the source after that. A code edit now rebuilds only the last few layers.
Key idea Put what changes least near the top. Keep apt-get update and apt-get install in the same RUN, so a cached update never goes stale.
.dockerignore decides what the builder even sees docker build sends a build context to the builder. Without a .dockerignore, that is the whole folder: .git, node_modules, local build output, maybe a .env file.
A large context is slow to send. Files that change on every commit, like .git, break the cache of COPY . . for no reason. And a stray .env can end up inside the image.
Key idea Start .dockerignore with .git, dependency folders, build output and any secret files. It uses patterns like .gitignore.
Build with the toolchain, ship without it A multi-stage Dockerfile has more than one FROM. The first stage (FROM golang AS build, or a Node or JDK image) has compilers and dev dependencies, and runs the build and tests.
The last stage starts from a small base and takes only the result: COPY --from=build /out/app /app. The toolchain, source and caches stay behind in the build stage and are not in the image you push.
A slim or distroless base has less to patch Every package in the base image is something a scanner can flag and someone must patch. A slim image (for example a -slim Debian tag or Alpine) drops most of them. A distroless image keeps only the runtime libraries, with no package manager and no shell.
Smaller images also pull faster on every node that starts the Pod. The cost: you cannot kubectl exec into a shell. Use kubectl debug to attach an ephemeral debug container instead.
Key idea Pick the smallest base your app runs on. Debugging moves from the image into a separate debug container.
Set USER to a numeric non-root ID Without USER, the process runs as root inside the container. If the app is broken into, the attacker starts as root, one step closer to the node.
Add USER 10001 (or the nonroot user that distroless images provide). Use a numeric ID: with runAsNonRoot: true, the kubelet can only verify that the user is not root when the image gives a number, and it refuses to start a container with a non-numeric user name.
Key idea Files the app must write need to belong to that UID. Use COPY --chown, or write to a mounted volume.
Shell form swallows SIGTERM To stop a Pod, the kubelet asks the runtime to send SIGTERM to PID 1 in the container. With shell form, CMD node server.js, PID 1 is /bin/sh -c, and the shell does not pass the signal on.
The app never hears it, keeps serving, and is killed with SIGKILL when the grace period (30 seconds by default) runs out. Open requests are cut off and every rollout waits the full timeout.
Watch out Use exec form: ENTRYPOINT ["/app"] or CMD ["node", "server.js"]. If a wrapper script is needed, end it with exec "$@". Run one main process per container.
A deleted secret is still in an earlier layer COPY .npmrc, RUN npm ci, RUN rm .npmrc looks clean, but the token stays in the layer made by COPY. ARG and ENV values persist in the image metadata too. Anyone who pulls the image can read them with docker history or by unpacking layers.
BuildKit secret mounts solve this. RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci makes the file visible only during that one RUN. Pass it with docker build --secret id=npmrc,src=$HOME/.npmrc.
Watch out Secrets never go in ARG, ENV or COPY. Mount them for the one step that needs them. Runtime secrets come from Kubernetes, not the image.
Same input, same image FROM node:22 can mean a different image next week, and npm install without a lockfile can pull different versions. Then a rebuild of the same commit is not the same image.
Pin the base by digest (FROM image:tag@sha256:...) and let a bot such as Dependabot or Renovate bump it. Install from lockfiles. Set SOURCE_DATE_EPOCH so BuildKit writes fixed timestamps. Why digests matter for trust is in the supply chain lesson.
Watch out A digest pin also stops automatic base updates, so pair it with a bot that proposes new digests.
Kubernetes ignores HEALTHCHECK A HEALTHCHECK line in the Dockerfile is read by Docker. The kubelet does not run it. A Pod with a hung app stays Running and Ready unless the Pod spec has probes.
Define readinessProbe, livenessProbe and, for slow starters, startupProbe in the manifest. An exec probe needs its command inside the image, which a distroless image may not have, so prefer httpGet or grpc probes.
Fast to build, small to ship, clean to stop Order layers from least to most often changed, and filter the context with .dockerignore. Build in one stage and ship a small, pinned base in another.
Run as a numeric non-root user, start the app in exec form, mount build secrets instead of copying them, and leave health checks to Kubernetes probes.
Summary Before you push, run docker history on the image: every layer should be something you meant to ship.