Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 21/ 160
02. Dockerfiles & Image Building

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.

BeginnerLayers · CoW3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Concrete Layers setup

docker build -t demo:inspect . && docker history --no-trunc demo:inspect && docker image inspect demo:inspect --format {{json .RootFS.Layers}}

Key takeaway: Prove CoW with 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.

Deliberate failure test

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
Design

1. 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.

The image is a frozen stack of shared templates; a running container adds its own writable top sheet.
Key numbers at this step
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 container in 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

Explicit Layers
Best for: Understand image layers, copy-on-write containers, and why layer order affects size and cache, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived CoW experiments with low blast radius.

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.

Managed Image IDs
Best for: Teams that centralize Image IDs policy across many workloads.

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 skipping compare 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Linux Kernel Primitives: Control Groups (cgroups) & Union File Systems
Next topic
Anatomy of a Dockerfile & Build Context