JuniorDocker

How does the Docker build cache work, and how do you order a Dockerfile to use it well?

What they are really testing: Whether you understand layer caching and the dependency-before-source ordering that makes builds fast. A poorly ordered Dockerfile rebuilds everything on every code change.

A real interview question

How does the Docker build cache work, and how do you order a Dockerfile to use it well?

What most people say

drag me

Docker caches layers, so builds are faster the second time.

It knows caching exists but not how to exploit it. The actual skill, copying dependency manifests before source so code edits do not rebuild dependencies, is the difference between a 5-second and a 5-minute rebuild.

The follow-ups they ask next

  • Why copy package.json and install before copying the rest of the source?

    So a source-only change reuses the cached dependency-install layer. Dependencies change rarely; code changes constantly. Copy-then-install at the top means every code edit reinstalls everything, which is slow.

  • What does a .dockerignore do for caching and image size?

    It excludes files (node_modules, .git, build output) from the build context, so they do not bust COPY-layer caches or bloat the image, and it speeds up sending the context to the daemon.

What the interviewer is listening for

  • Knows a change busts all later layers
  • Copies deps before source deliberately
  • Uses .dockerignore

What sinks the answer

  • Only "it caches layers"
  • Copies all source before installing deps
  • Never mentions .dockerignore

If you genuinely do not know

Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.

Each instruction is [a cached layer, reused if unchanged]; [a change invalidates every layer after it]. So I [copy the dependency manifest and install deps before copying source], so [code edits reuse the cached dependency layer]. I add [a .dockerignore to keep junk out of the context and stable COPY layers].

Keep going with docker

All 336 cloud engineer questions

Knowing the answer is not the same as recalling it under pressure

Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.

Start free