Skip to the page

Containers in depth · mid

Dockerfile review, small images and base image choice

A good production image is pinned, cached well, multi-stage and small. Know how to read a Dockerfile for slow builds and large size, and how to choose Alpine, slim or distroless.

The notes

What you must know

  • latest floats, so builds are not repeatable. Pin a version and a slim variant, ideally by digest.
  • A secret in ENV is stored in the image config and history. Pass secrets at runtime or use BuildKit RUN --mount=type=secret for build time. Rotate any secret that was baked in.
  • Cache: copy the dependency file first, install, then copy the code. COPY . . before the install makes any change invalidate the install layer. Use cache mounts for package caches.
  • Every instruction adds a layer. Deleting a file in a later layer only hides it. Clean in the same RUN or use multi-stage builds.
  • A bad .dockerignore makes COPY . . include .git and test data, which can grow an image from 180 MB to 1.6 GB. docker history and dive show layer sizes.
  • Multi-stage: build in a stage with build tools, copy only the result into a small final stage with a JRE or runtime, and set a non-root USER.
  • Alpine uses musl libc, so some wheels and native modules behave differently and DNS behaviour differs. Debian slim uses glibc and has the fewest surprises. Distroless has no shell or package manager: the smallest attack surface, harder to debug.
  • Large images slow scale-ups because every new node must pull them. Keep images small, keep warm nodes, and use a pull-through cache or registry close to the nodes.

The red pen

Common traps: mistake → fix

  • Mistake: Delete files in the last layer to make the image smaller

    Fix: the bytes stay in the earlier layer.

  • Mistake: Alpine is always the best base

    Fix: musl can cause surprises; test it.

  • Mistake: ENV is a safe place for a password

    Fix: it is readable in image metadata.

Easy to hard

Questions interviewers ask, easy to hard

  1. Review this Dockerfile: builds take 8 minutes and the image is 1.4 GB.
  2. Write a production Dockerfile for a Spring Boot service.
  3. The image grew from 180 MB to 1.6 GB. What do you do?
  4. Alpine, slim or distroless?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview