How Docker Images Work (Read-Only Layers & Copy-on-Write)
Understand image layers, copy-on-write containers, and why layer order affects size and cache.
In plain words
The image is a frozen stack of shared templates; a running container adds its own writable top sheet.
Think of it like…
Think of transparent architectural overlays: a change drawn on the top sheet hides what is below without erasing the paper underneath.
Where you’ve seen it
In practice, docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}} is the concrete move for this job: understand image layers, copy-on-write containers, and why layer order affects size and cache. The release is not accepted until compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container confirms the result.
Words you’ll hear
- Layers
- The implementation control in
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}for understand image layers, copy-on-write containers, and why layer order affects size and cache. - CoW
- The result to observe with
compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container, rather than infer from a successful command alone. - Image IDs
- The production boundary exposed when installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink.
Visual diagram
Theory & deep dive
Layers control path
The concrete mechanism is docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}. The observable result is compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container; if those disagree, inspect the runtime evidence before changing unrelated settings.
Understand image layers, copy-on-write containers, and why layer order affects size and cache. Keep Layers, CoW and Image IDs in the same deployment record so the intent and the observed state cannot drift apart.
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}
compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container, not with command success alone.Image IDs failure boundary
Layer identity explains caching and storage: two images can share identical base blobs, while a late source change normally invalidates every later layer. It also explains why deleting a file in a newer layer cannot reliably shrink space held by an earlier layer. The narrow failure to design for is installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink.
Accept this compromise explicitly: Shared immutable layers make pulls and starts efficient; copy-on-write is a poor design for large, frequently modified state that belongs in a volume. The controlled check-compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container-also identifies the point where a different design is safer.
Exercise this failure safely: installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Layer identity explains caching and storage: two images can share identical base blobs, while a late source change normally invalidates every later layer. It also explains why deleting a file in a newer layer cannot reliably shrink space held by an earlier layer.
- Intent
- Layers
When to use it & trade-offs
What it is
An image is an ordered manifest of immutable content-addressed layers plus configuration. On a common overlay driver, reads fall through the layers and a first write copies a file into the container’s writable upper layer, which is why image contents remain unchanged when a container edits a file.
Why it matters: Layer identity explains caching and storage: two images can share identical base blobs, while a late source change normally invalidates every later layer. It also explains why deleting a file in a newer layer cannot reliably shrink space held by an earlier layer.
When to reach for it
- The fit is strongest for this job: understand image layers, copy-on-write containers, and why layer order affects size and cache. Pair it with
compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started containerin the runbook. - Production Layers changes should make CoW observable through
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}, rather than trusting the runtime default. - Keep a rollback example for the case where installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink.
When to avoid or be careful
- Do not accept command success while the system can still fail by installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink.
- Avoid erasing Image IDs protection merely to dodge this cost: “Shared immutable layers make pulls and starts efficient; copy-on-write is a poor design for large, frequently modified state that belongs in a volume”.
- Do not replace the evidence path-
compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container-with a screenshot or a mutable tag.
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}: practical choices
Strengths: An image is an ordered manifest of immutable content-addressed layers plus configuration. On a common overlay driver, reads fall through the layers and a first write copies a file into the container’s writable upper layer, which is why image contents remain unchanged when a container edits a file.
Watch out: Shared immutable layers make pulls and starts efficient; copy-on-write is a poor design for large, frequently modified state that belongs in a volume.
Strengths: It can begin with docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}} and fewer surrounding controls.
Watch out: It silently accepts installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink, which makes the result hard to reproduce after promotion.
Strengths: Layer identity explains caching and storage: two images can share identical base blobs, while a late source change normally invalidates every later layer. It also explains why deleting a file in a newer layer cannot reliably shrink space held by an earlier layer.
Watch out: The platform adds an ownership and debugging layer; retain compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}
- compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container
- Failure signature: installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink
- Trade-off: Shared immutable layers make pulls and starts efficient; copy-on-write is a poor design for large, frequently modified state that belongs in a volume.
Benchmarks & rules of thumb
- 1 writable upperdir
- per container on an overlay driver
- N immutable layers
- referenced by the image manifest
- 64 layers
- common historical compatibility target, although the effective limit is implementation-dependent
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}as proof and skippingcompare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container. - Installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink
- Optimizing Layers without accepting that shared immutable layers make pulls and starts efficient; copy-on-write is a poor design for large, frequently modified state that belongs in a volume.
Likely interviewer follow-ups
- Why does
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}address Layers here rather than through application code? - How would you distinguish healthy CoW from the failure “installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink” using
compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container? - When would this trade-off-“Shared immutable layers make pulls and starts efficient; copy-on-write is a poor design for large, frequently modified state that belongs in a volume”-justify a different Image IDs design?
docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}-and one proof-compare the image RootFS layer count with the visible history entries and the directory count under the overlay MergedDir for a started container. Then name the boundary: installing a large temporary compiler in one RUN layer and deleting it in the next, expecting the image payload to shrink.Knowledge check
Put it into practice
Practice related interview Q&A
Apply How Docker Images Work (Read-Only Layers & Copy-on-Write) with generative AI interview Q&A and hands-on practice.