Skip to the workspace
Pebellslearn by building
Underlying Technologies  ›  Capstone Project

Layers and Union Filesystems

Union Filesystems 14 minAn image is a stack of read-only layers merged into one view, and each container adds a thin writable layer on top where every change it makes is stored using copy-on-write.

docker / Layers and Union Filesystems

Explore each scene at your own pace.

01 · The rule

The rule

Layers and Union Filesystems

You start ten containers from the quicknotes image, which is 150 MB. Your disk does not lose 1.5 GB; it barely moves. And when you pull a new version of the image, only a small part downloads. Both tricks come from how images are stored: as layers merged by a union filesystem.

The rule

An image is a stack of read-only layers; a container adds one thin writable layer.

Each instruction in a Dockerfile that changes files (RUN, COPY, ADD) produces a new layer: a record of just the files it added, changed or deleted.

FROM python:3.12-slim                                # base layers
COPY requirements.txt .                              # layer: one file
RUN pip install --no-cache-dir -r requirements.txt   # layer: libraries
COPY . .                                             # layer: app code

A union filesystem (on modern Linux, Docker's overlay2 storage driver) stacks these layers and shows them as one ordinary folder tree. When a container starts, Docker puts one empty writable layer on top. From that come three rules:

  • Sharing. All ten quicknotes containers read the same image layers from disk. Only their small writable layers differ.
  • Copy-on-write. When a container changes a file that lives in an image layer, the file is first copied up into its writable layer and changed there. The image is untouched.
  • Deletes only hide. Removing a file that exists in a lower layer just records a "whiteout" marker on top. The bytes stay in the lower layer.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.