Case File: The Fat Image
Sabi Logistics' container image is over a gigabyte, takes ten minutes to rebuild after a one-line change, runs as root and has a live API key inside it. Rewrite the Dockerfile: pinned small base images, a multi-stage build, a cache-friendly layer order, a non-root user, a health check and no secrets. The details are generated for you alone.
Sabi Logistics · Delivery 1
The Fat Image
Every deployment takes ten minutes, and the security review has found a live key inside the image
Ngozi Lead developer · Phone call
Ngozi. We have started putting the API in containers and I have a confession: the Dockerfile is the one I wrote in my first week of learning Docker. The image is over a gigabyte. After I change one line of code it rebuilds everything, which takes ten minutes. And Emeka, who read the security review, says the container runs as root and there is a live key somewhere inside it.
Chidi wants this on the agenda for Monday. Can you rewrite it so that it builds quickly, comes out small and is safe to run?
The brief
What you need to know first.
- A Dockerfile is a recipe. Each instruction (
FROM,COPY,RUN…) makes a layer, and Docker caches layers. If a layer and everything before it is unchanged, Docker reuses the cache. Everything after a changed layer is rebuilt. - Order matters for speed. Copy the dependency files (
package.json, the lock file) and install first; copy the source afterwards. Source changes often, dependencies rarely. - Pin your base image.
FROM node:latestmeans something different every month. Use a version such asnode:20.11-alpine: alpine and slim variants are much smaller. - Multi-stage builds. The first stage (
FROM ... AS build) installs everything and builds. The last stage starts clean and brings over only the result withCOPY --from=build. Compilers and sources never reach the final image. - Run as a normal user (
USER node), install production dependencies only (npm ci --omit=dev), and setNODE_ENV=production. - Document and check the app:
EXPOSEthe port and add aHEALTHCHECK. - Use the exec form of
CMD:CMD ["node", "dist/server.js"]runs node directly so it receives stop signals. - Never put a secret in an
ENVorARG. Anyone who can pull the image can read it.
Fix the file until every requirement is met. The checker reads instructions, so spacing does not matter.
The flag: when everything is met your change ticket reference appears. Submit it at the bottom of the page.
Preparing the server…
Ngozi, after the new image builds in forty seconds: “Brilliant. But Emeka’s question is the hard one: the key was in the image for three weeks, and the image is in our registry. Is it enough to rebuild without it?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Ngozi: “So removing it from the file does not remove it from where it already went.”
What a senior engineer would do next. Treat the key as compromised: revoke it and issue a new one first, because rebuilding does not delete the old image layers from the registry, from other people’s machines or from any cache. Then delete the old tags from the registry, scan the repository and the images for other secrets (tools such as gitleaks and trivy do this for free), and pass the new key at run time (an environment variable from a secret store, or a Kubernetes Secret). Add an image scan to the pipeline so this is caught on the next pull request, and add a .dockerignore so that .env files and .git are never copied into an image. Pin base images by version, and update them on a schedule, because a pinned image stops receiving security fixes until you change the pin.
The ideas to keep.
- Order layers by how often they change. Dependencies first, source last.
- Ship only what runs. Multi-stage builds leave the tools behind.
- Pin, and keep updating. A pinned image is repeatable; an old one is vulnerable.
- A leaked secret is revoked, not deleted. Rotate first.
Next case: the three containers.
Write the change note
Describe what was wrong with the Dockerfile and what you changed: the base image, the build stages, the layer order and why it speeds up rebuilds, the user, the health check, and what you did about the exposed key.
🧪 Want to practise this on your own computer? See how to set up your lab →