DevOps Engineer

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.

45 min

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:latest means something different every month. Use a version such as node: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 with COPY --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 set NODE_ENV=production.
  • Document and check the app: EXPOSE the port and add a HEALTHCHECK.
  • 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 ENV or ARG. 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…

🔒 The debrief unlocks when you close the case. Finish the lab above.

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.

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 3

Why copy package.json and run npm ci BEFORE copying the rest of the source?

Report a problem

About: /lessons/case-the-fat-image