Lesson 10 of 30
Layer caching only makes sense once you know what a layer actually IS — a union filesystem is the mechanism that stacks a dozen read-only diffs into one filesystem a running container can read and write like a normal disk.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
A running container reads a file that exists only in a read-only image layer three levels down. What does the union filesystem do?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
The Dockerfile-layers lesson treated "every instruction produces a layer, and layers get cached" as a given. This lesson explains the mechanism underneath that fact: a union filesystem (Docker's default implementation is overlay2) is how Docker takes a stack of independent, read-only layer diffs and presents them to a container as one single, ordinary-looking filesystem.
Two rules explain the whole thing:
Reads fall through the stack top to bottom: asking for a file that only exists in a lower read-only layer works transparently, because the union filesystem presents all layers merged into one view.
container layer (writable) <- empty until something changes
──────────────────────────────
layer 3: RUN pnpm build (readonly)
layer 2: RUN pnpm install (readonly)
layer 1: COPY package.json (readonly)
layer 0: FROM node:20-alpine (readonly)
The layer stack: three read-only image layers, plus one empty writable container layer on top.
Reading /app/package.json from inside the running container falls straight through the empty writable layer to layer 1, where it actually lives — no copying happens. But the moment the container process writes to that same file, the union filesystem performs copy-on-write: it copies the file up into the writable container layer first, then applies the write there. The original copy in layer 1 is untouched — it's read-only, permanently — and every other container built from the same image still sees the original, unmodified version.
docker diff shows exactly this writable layer — it lists every file a running container has added, changed, or deleted relative to its image, which is the concrete, inspectable version of the abstract "copy-on-write" story above."What actually happens when a running container writes to a file?" tests whether layer caching is understood as a mechanism (union filesystem, read-only layers, copy-on-write) or just memorized as "Docker caches layers, it's faster." The strong answer names copy-on-write specifically and explains why it makes containers both fast to start and safe to discard.