Layers, overlayfs and copy-on-write

Intermediate · 13 min

How read-only image layers and a thin writable layer become one filesystem with overlayfs, what copy-up and whiteouts cost, and where it all lives on disk.

Before thisImage names: repositories, tags and digests

WhatsCoding · 13 min

Layers, overlayfs and copy-on-write

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by WhatsCodingReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An image's layers are read-only changesets. Each container adds one thin writable layer on top, and overlayfs presents the stack as one directory tree.
  • Layers are shared by digest. Every image built on debian:trixie-slim reuses the same 29.8 MB base layer on disk and over the network.
  • Writing to an image file for the first time copies the whole file up into the writable layer, even to change one byte. That's why databases and other heavy writers belong on volumes.
  • Deleting a file never shrinks lower layers. It adds a whiteout that hides the file, so a file deleted in a later Dockerfile step still ships in the image.
  • In image tar layers, a whiteout is a .wh.<name> entry and an opaque directory is .wh..wh..opq. On a live overlay mount they're special device files and xattrs.
  • Engine 29's containerd image store keeps layers as containerd snapshots under /var/lib/containerd, while upgraded hosts may still use overlay2 under /var/lib/docker/overlay2.

Fifty containers, one copy

Start fifty containers from a 1 GB image and your disk doesn’t lose 50 GB. They start in milliseconds, and pulling a second image based on the same Debian release downloads almost nothing. All three come from one design: an image isn’t a disk image, it’s a stack of read-only layers, and each container gets a thin writable sheet on top. The same design explains the classic surprises: images that won’t shrink when you delete files, and databases that crawl inside a container.

The devops Dockerfile lesson showed that layers exist. This one is about how the kernel stacks them and what each operation costs.

Union mounts with overlayfs

overlayfs is a Linux filesystem that merges directories. You give it one or more read-only lower directories, one writable upper directory, a work directory for internal use, and it mounts the merged view:

# the shape of the mount, from Docker's overlay2 docs (not run here)
mount -t overlay overlay \
  -o lowerdir=/l3:/l2:/l1,upperdir=/c/upper,workdir=/c/work /c/merged

For a container, the image layers are the lowers, ordered top to bottom, the container’s writable layer is the upper, and merged becomes its root filesystem. Docker’s overlay2 driver supports up to 128 lower layers. Reads go to the topmost layer that has the file. It’s like transparent sheets on an overhead projector: you see the top-most mark at each spot, and you can only draw on the top sheet.

Layers are shared by digest

Layers are content-addressed, so two images that start from the same base store and download that base once. I pulled the amd64 manifests for three official images:

python:3.14-slim  layer 0  sha256:40748add9f4f…  29,842,182 bytes
debian:trixie-slim layer 0 sha256:40748add9f4f…  29,842,182 bytes
nginx:1.31        layer 0  sha256:40748add9f4f…  29,842,182 bytes

The same blob. On a host with all three, that 29.8 MB layer exists once. This is a good reason for a team to agree on a few base images: the fewer distinct bases, the more sharing.

Copy-up: the cost of the first write

The lower layers are read-only. When a container writes to a file that lives in an image layer, overlayfs first copies the entire file into the upper layer, then applies the write there. Docker’s docs stress that this works “at the file level rather than the block level”, so changing one byte of a 2 GB file copies 2 GB. Later writes hit the copy.

Builds work the same way, one layer per step. In python:3.14-slim, the Debian base layer holds /var/lib/dpkg/status at 69,788 bytes. The next layer’s apt-get install updated a few package records, and that layer contains the whole file again at 72,351 bytes:

$ tar -tzvf pyslim-l0.tgz | grep 'var/lib/dpkg/status$'
-rw-r--r--  0 0  0  69788 Nov 23 05:30 var/lib/dpkg/status
$ tar -tzvf pyslim-l1.tgz | grep 'var/lib/dpkg/status$'
-rw-r--r--  0 0  0  72351 Nov 24 10:23 var/lib/dpkg/status

That’s why anything that writes heavily belongs on a volume, which is a plain directory mounted past overlayfs: databases, upload directories, caches. It’s also why the container filesystem is ephemeral: the upper layer is deleted with the container.

Whiteouts: deleting without deleting

A lower layer can’t be changed, so deletion is recorded as a marker in a higher layer. The OCI image spec defines how a layer tarball says it: an entry named .wh.<name> deletes <name> from the layers below, and .wh..wh..opq inside a directory hides everything the lower layers had there. On a live overlay mount the kernel uses its own form, a character device with device number 0/0, plus an extended attribute for opaque directories, and the runtime translates between the two.

The four python:3.14-slim layers I listed had no whiteouts at all, since the official images clean up inside the same RUN. So I built two small layers by hand and stacked them with a Python stand-in that applies the OCI rules in order:

L1.tar: etc/app.conf (port=80), app/big.bin (5,000,000 bytes), cache/a, cache/b
L2.tar: etc/app.conf (port=8080), app/.wh.big.bin, cache/.wh..wh..opq, cache/c

after L1.tar: {'etc/app.conf': 8, 'app/big.bin': 5000000, 'cache/a': 1, 'cache/b': 1}
after L2.tar: {'etc/app.conf': 10, 'cache/c': 1}
layer sizes on disk: {'L1.tar': 5007360, 'L2.tar': 10240}

The merged view no longer has big.bin, but L1.tar still holds its 5 MB, and anyone who pulls the image downloads it. That’s the “I deleted the file but the image is still big” bug, and the reason secrets must never touch a layer. Deleting them later doesn’t remove them. docker diff api shows a running container’s own changes as A (added), C (changed) and D (deleted, a whiteout).

Where it lives on disk

That depends on the image store:

  • Classic overlay2 (hosts upgraded from before Engine 29): /var/lib/docker/overlay2/<id>/ holds diff/ (the layer’s files), link (a short name), lower (the chain of short names below it), and for containers merged/ and work/. Short symlinks in /var/lib/docker/overlay2/l/ keep the mount options under the page-size limit. docker inspect shows them under GraphDriver.
  • containerd image store (the default on fresh Engine 29 installs): layers are containerd snapshots under /var/lib/containerd, managed by its overlayfs snapshotter. The mechanism is the same overlay mount. Docker’s docs note this store also keeps compressed layers, so it uses more disk, and that it enables multi-platform images locally. docker info shows which store you have.

About the video

The embed is all of WhatsCoding’s “Understanding OverlayFS | OverlayFS | Docker”, January 2025, 13 minutes. Its chapters cover an introduction to overlayfs, copy-up and whiteouts, setting up an overlay mount, a file-modification example, a whiteout demonstration and the editable upper layer. It builds overlay mounts by hand rather than looking inside Docker’s directories, and doesn’t cover layer sharing or the containerd snapshotter, which the lesson adds. English was judged from its “English (US) original” audio track. Techi Nik’s video in deeper adds a live Podman container’s overlay mount.

What was run: the three base-layer digests came from Docker Hub’s registry API, the python:3.14-slim layers were downloaded and listed with tar, and the whiteout example was a Python stand-in that applies OCI layer rules to two hand-made tar files. No overlay mount was made, since this is macOS. The overlay2 layout, copy-up behaviour and 128-layer limit are from Docker’s overlayfs driver docs, and the whiteout formats from the OCI layer spec and the kernel’s overlayfs docs, checked 2026-11-28.

Where this fits

Layers belong to one platform’s image. Next, how one tag covers many platforms: manifests, indexes and multi-platform images.

Check yourself

  1. 01Your Dockerfile downloads a 400 MB archive in one RUN and deletes it in the next. Why is the image still 400 MB bigger?

    The first RUN's layer contains the archive. The second layer only adds a whiteout that hides it. Download, use and delete in the same RUN, or use a multi-stage build.

  2. 02A Postgres container writing to its data directory in the container layer is slow on first touch of each file. Why?

    overlayfs copies a whole file from the image layer up to the writable layer the first time it's written. Put the data directory on a volume, which bypasses overlayfs.

  3. 03Ten containers run from the same 500 MB image. Roughly how much disk do they use?

    About 500 MB for the shared image layers plus each container's own writable layer, which holds only what that container changed. The image isn't copied per container.

  4. 04In an image layer tarball you find app/.wh.config.yaml. What does it mean?

    It's a whiteout: when the layers are stacked, app/config.yaml from any lower layer is hidden, as if deleted. The entry itself never appears in the container.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark