Layering & Copy-on-Write

Difficulty: Advanced

Question

How do Docker image layers and the Copy-on-Write (CoW) mechanism work, and what are the practical consequences?

Answer

A Docker image is an ordered stack of read-only layers, each one a filesystem diff produced by an instruction like RUN, COPY or ADD. When you start a container, Docker adds a thin writable layer on top and presents the union of all layers as one filesystem. Files are shared from the read-only layers until a container modifies one; only then is that file copied up into the writable layer (Copy-on-Write), so many containers can share the same image on disk.

How the union filesystem is assembled

On Linux the classic storage driver is overlay2, built on the kernel's OverlayFS. Recent Docker Engine releases default fresh installs to the containerd image store, which uses containerd's overlayfs snapshotter; the mechanism is the same.

  • lowerdir: the image layers, read-only and shared by every container using that image.
  • upperdir: the container's private writable layer.
  • merged: the view the container process actually sees.
  • workdir: scratch space OverlayFS needs internally.

Each layer is content-addressed by a SHA-256 digest, so two images built on the same node:22-alpine base store that base only once, and docker pull skips layers it already has.

What happens on read, write and delete

  1. Read: the file is looked up from the top layer down; the first match wins. No copying.
  2. First write to an existing file: the whole file is copied up from the lower layer into upperdir, then modified. Changing one byte of a 2 GB file copies 2 GB.
  3. New file: written directly into upperdir.
  4. Delete: a whiteout entry is created in the upper layer that hides the lower file. The original bytes still exist in the image.

That last point catches people in Dockerfiles too. RUN apt-get install ... followed by a separate RUN rm -rf /var/lib/apt/lists/* does not shrink the image, because the files still live in the earlier layer. Cleanup has to happen in the same RUN that created the files.

Why it matters in production

  • Fast startup and low disk use: starting a container only creates an empty upper layer; nothing is copied. Fifty replicas of a service on one host share one copy of the image.
  • Write-heavy workloads suffer: databases, log files and upload directories should not live in the writable layer. Copy-up of large files is slow and the data disappears with the container. Use volumes, which bypass the storage driver.
  • Layer order drives build cache: put rarely changing steps (installing OS packages, npm ci after copying only package*.json) before frequently changing ones (COPY . .) so rebuilds reuse cached layers.

Inspecting layers

  • docker history <image> lists each layer, its size and the instruction that created it. Instructions like ENV, EXPOSE and CMD only change metadata and show 0B.
  • docker ps -s shows each container's writable layer size plus the virtual size including the image.
  • docker image inspect -f '{{json .RootFS.Layers}}' <image> prints the layer digests.
A running container's filesystem. Only the top layer is writable; everything below is shared and read-only.
  1. Container writable layer (upperdir): Copied-up files, new files and whiteouts for this container only.
  2. COPY . /app: Application source code layer.
  3. RUN npm ci: node_modules installed from the lockfile.
  4. COPY package*.json ./: Small layer that keys the dependency cache.
  5. Base image node:22-alpine: OS and runtime layers shared across many images.

Code examples

Watching copy-on-write happen

# Start two containers from the same image
docker run -d --name a nginx:1.27
docker run -d --name b nginx:1.27

# Modify a file that lives in an image layer, in container a only
docker exec a sh -c 'echo "# edited" >> /etc/nginx/nginx.conf'

# docker diff shows the copied-up file in container a only
docker diff a | grep nginx.conf
docker diff b | grep nginx.conf || echo "b untouched"

# Writable layer sizes per container
docker ps -s --filter name=a --filter name=b

Editing the config in container a copies that file into a's writable layer; container b still reads the original from the shared image layer.

Delete in the same layer, not a later one

# Dockerfile (bad): the apt lists are still stored in the first layer
RUN apt-get update && apt-get install -y curl
RUN rm -rf /var/lib/apt/lists/*

# Dockerfile (good): created and removed in one layer
RUN apt-get update \
 && apt-get install -y --no-install-recommends curl \
 && rm -rf /var/lib/apt/lists/*

A delete in a later layer only adds a whiteout, so the bytes still ship with the image. Combining the steps keeps them out of every layer.

Key points

  • Image layers are read-only and content-addressed by SHA-256, so identical layers are stored and pulled once.
  • Copy-up copies the entire file on first write, which is why write-heavy data belongs in volumes.
  • Deleting a file in a later layer adds a whiteout; it never reduces image size.
  • Only RUN, COPY and ADD add filesystem content; other instructions are metadata.
  • overlay2 is the classic Linux storage driver; recent Engines default new installs to the containerd image store with an overlayfs snapshotter.

Common follow-up questions

  • Why should a database never write to the container layer?
  • How do multi-stage builds reduce final image size?
  • What is a whiteout file?
  • How does layer ordering affect build cache hits?

Interview tips

  • Name the OverlayFS pieces (lowerdir, upperdir, merged) to show you know what is under the abstraction.
  • Mention that copy-up is whole-file; it explains the performance advice about volumes.
  • Tie it to a Dockerfile habit, such as cleaning apt caches in the same RUN, to show practical value.

Concepts covered

Image layers, Copy-on-Write, OverlayFS, overlay2, Whiteout files

More Docker & Containers questions