QA
Intermediate LevelDocker & Deployment

How does the Overlay2 Storage Driver and Copy-on-Write (CoW) filesystem operate?

❓ Interview Prompt: Explain OverlayFS lower/upper/merged directories, Copy-on-Write semantics, and how container writable layers work on top of read-only image layers.

Docker's default storage driver (overlay2) uses the Linux OverlayFS kernel module to implement the layered image filesystem:

OverlayFS Directory Structure:

•
lowerdir: Stack of read-only image layers (immutable).
•
upperdir: Writable container layer (per-container, discarded on docker rm).
•
workdir: Internal OverlayFS working directory (required by kernel).
•
merged: The unified view presented to the container process — combines all layers.

Copy-on-Write (CoW) Semantics:

When a process inside the container reads a file that exists in a lower layer, it reads directly from the lower layer — no copy needed.

When a process writes to a file from a lower layer, OverlayFS:

1
Copies the entire file from the lower layer to the upper (writable) layer.
2
The container then reads/writes the copy in the upper layer.
3
The original in the lower layer remains untouched.

Performance Implication:

The first write to a large file triggers a full copy to the upper layer (copy-up) — this can cause latency spikes for write-heavy workloads. Solution: use Docker Volumes for I/O-intensive paths.

Code Implementation Example

bash
# Inspect the overlay2 layer directories for a container
CONTAINER_ID=$(docker inspect myapp --format '{{.Id}}')
docker inspect myapp --format '{{.GraphDriver.Data}}'
# {
#   "LowerDir": "/var/lib/docker/overlay2/abc123/diff:...",
#   "MergedDir": "/var/lib/docker/overlay2/xyz789/merged",
#   "UpperDir": "/var/lib/docker/overlay2/xyz789/diff",
#   "WorkDir": "/var/lib/docker/overlay2/xyz789/work"
# }

# See the unified merged view (what container sees)
ls /var/lib/docker/overlay2/xyz789/merged/

# Demonstrate CoW: writing to a file copies it to UpperDir
docker exec myapp sh -c "echo 'modified' > /etc/hosts"
ls /var/lib/docker/overlay2/xyz789/diff/etc/
# hosts  ← copy-up occurred, modified version is in UpperDir
💡overlay2 presents a merged view of read-only lower layers + writable upper layer using Linux OverlayFS.
🎯 Interview Key Takeaway

OverlayFS stacks read-only image layers (lowerdir) with a per-container writable layer (upperdir). First write triggers copy-up — use Volumes for high-I/O paths to avoid CoW overhead.

#OverlayFS#Copy-on-Write#Storage Driver#Layers#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?