Sub topic 12
Union filesystems and copy-on-write under the container
This chapter assumes you can run docker run and docker build, and that you know an image is a stack of read-only layers. Here you will see how Docker presents those layers as one filesystem, and what happens when a running container changes a file.
How layers become one filesystem
A container needs a single directory tree, but an image is many separate layers. Docker uses a union filesystem to merge them. On most Linux hosts the storage driver is overlay2, which is built on the kernel's OverlayFS.
OverlayFS has these parts:
- lowerdir: the read-only image layers, stacked in order.
- upperdir: one writable directory that belongs to the container.
- merged: the combined view that the container sees as
/.
When the same path exists in several layers, the highest one wins. A file in an upper layer hides the same path in a lower layer.
Copy-on-write
Layers are shared, so Docker does not copy them when it starts a container. It uses copy-on-write: a file is copied only at the moment a container modifies it.
- Read: Docker serves the file from the layer that holds it. Nothing is copied.
- Modify an existing file: Docker first copies the whole file from the lower layer into the container's writable layer. The change is then applied to that copy.
- Create a new file: Docker writes it straight into the writable layer.
- Delete a file: Docker does not remove the lower copy. It writes a marker in the writable layer, called a whiteout, that hides the file in the merged view.
The copy is of the whole file, not only the changed bytes. Appending one line to a 2 GB file copies 2 GB first.
Whiteouts explain the behavior you saw earlier in this tutorial. A rm in a later RUN step creates a whiteout in a new layer. The original bytes stay in the earlier layer, so the image does not shrink.
Seeing it happen
You can watch the writable layer with docker diff. It lists the changes a container has made on top of its image.
docker run -d --name cow-demo debian:bookworm-slim sleep 600
docker exec cow-demo sh -c 'echo hello > /new.txt && echo x >> /etc/hostname && rm /etc/issue'
docker diff cow-demo
docker rm -f cow-demoThe output has one line per change. A is an added path, C is a changed path, and D is a deleted path. You should see A /new.txt and D /etc/issue. Directories that contain a change are listed as C.
What this means in practice
- The writable layer is temporary. When you remove the container, its upperdir is deleted. Use volumes for data that must last.
- Containers from one image share the lower layers. Ten containers of the same image store the image data once, plus ten small writable layers.
- Heavy writes are slow and large. Copying up big files, or writing many small ones, costs time and disk. Databases and logs belong on a volume, which bypasses the union filesystem.
Exercise
A container modifies a 500 MB file that comes from a lower image layer, changing only one byte. What does Docker do, and how much space does the container's writable layer use afterward?
Show answer
Docker copies the entire 500 MB file into the container's writable layer (copy-up) and applies the one-byte change to the copy. The writable layer uses about 500 MB. The original file stays unchanged in the read-only image layer and remains shared with other containers.
Exercise
You run rm /etc/issue inside a container. The file lives in a read-only image layer. How does the file disappear from the container's view, and does the image change?
Show answer
Docker records a whiteout marker in the container's writable layer. The merged view hides the file because of that marker. The image is unchanged, and the original file is still stored in its lower layer.