CoursesDocker from the kernel up
Puzzle 11 · 12 of 17
The container's own layer
Puzzle 10's overlay mount gives each container one writable directory on top of the image. Here's what that means for the files a program writes: who can see them, what keeps them, and the one command that throws them away.
All puzzles in this course
The problem
Puzzle 10 showed that all of a container's writes land in one upper directory. In practice that raises the questions you hit first with a real service. Your container writes a cache file, or worse, a database. A second container from the same image starts beside it: does it see the file? The container is restarted after a crash: is the file still there? And when you replace the container with a new one from the same image, as every deployment does?
See it
Scroll sideways to see the full diagram.
Two containers from notes:v1, box and w2, stand on the same read-only layers. Each gets its own upper directory: its writable layer.
Step 1 of 6box writes /tmp/draft.txt. The file lands in box's writable layer; the image's layers don't change.
Step 2 of 6w2 runs the same image, on the same machine. Predict: can w2 read /tmp/draft.txt?
Step 3 of 6No: "No such file or directory". w2's tree is its own writable layer over the shared layers, and box's layer isn't in it.
Step 4 of 6Stopping and starting box keeps its writable layer: after a restart, draft.txt is still there.
Step 5 of 6docker rmdeletes the container and its writable layer. A new container from notes:v1 starts with an empty one: draft.txt is gone for good.Step 6 of 6
The building block
The upper directory of a container's overlay mount is its writable layer. Docker makes a new, empty one for each container, over the same read-only image layers. On the run behind this page, two containers from notes:v1, box and w2:
$ docker exec box sh -c 'echo draft > /tmp/draft.txt'
$ docker exec w2 cat /tmp/draft.txt
cat: can't open '/tmp/draft.txt': No such file or directory
$ docker restart -t 1 box
$ docker exec box cat /tmp/draft.txt
draft
$ docker ps -s --format '{{.Names}} {{.Size}}'
w2 4.1kB (virtual 8.52MB)
box 12.3kB (virtual 8.53MB)
(one line for an unrelated container left out)
$ docker rm -f box
$ docker run --rm notes:v1 cat /tmp/draft.txt
cat: can't open '/tmp/draft.txt': No such file or directory
Line by line:
- w2 can't see box's file. Each container's tree is its own writable layer over the shared ones; another container's writable layer isn't in it.
- A restart keeps it. Stopping a container stops its processes; the writable layer stays on disk until the container is removed. (
docker restart -t 1stops and starts it, waiting at most a second for the program to exit.) docker ps -sshows each container's writable layer (the first number) next to the image's size (virtual). box's writable layer takes 12.3 kB (a 6-byte file plus the /tmp directory it sits in, counted in disk blocks); both share the 8.5 MB.docker rmdeletes the container and its writable layer. A new container from the same image starts with an empty one. Every deployment that replaces a container, and everydocker run --rm, throws its writable layer away.
On the host, the writable layer is an ordinary directory, the upperdir of puzzle 10's mount line; for box it was …/snapshots/114/fs. Docker's documentation warns not to change anything under /var/lib/docker by hand: Docker owns that folder and removes the writable layer with the container.
So the writable layer is for scratch files only. Anything that must survive the container, a database, uploads, a cache you want to keep, goes into a volume or a bind mount, which live outside the container's layers (Module 5, coming soon). And since every write to an image file costs a copy-up (puzzle 10), heavy writes are faster there too.
The analogy
Each flat gets its own sheet of tracing paper over the shared printed plan. A tenant's drawings are only on that flat's sheet; next door sees its own. Locking up for a holiday leaves the sheet on the plan. When the tenancy ends, the sheet goes in the bin, and the next tenant gets a fresh one. Anything worth keeping should have gone into the storage room in the basement.
Run it yourself
With notes:v1 from puzzle 9. Predict each cat before you run it.
docker run -d --name box notes:v1 sleep 1000
docker run -d --name w2 notes:v1 sleep 1000
docker exec box sh -c 'echo draft > /tmp/draft.txt'
docker exec w2 cat /tmp/draft.txt
docker restart -t 1 box; docker exec box cat /tmp/draft.txt
docker ps -s --format '{{.Names}} {{.Size}}'
docker rm -f box w2
docker run --rm notes:v1 cat /tmp/draft.txt
The output from the run behind this page is the listing above. Sizes depend on how your machine stores images: on a second daemon using Docker's older way of storing images (puzzle 12), the same steps showed box at 6B and w2 at 0B, counting file contents rather than disk blocks.
Check yourself
Sources
- Docker docs: What is a container? ("When a container starts, it uses the image’s layers as they are and adds one thin writable layer of its own on top. Anything the running app writes, such as a new to-do in its database, goes into that writable layer, never into the image.")
- Docker docs: Storage drivers ("All writes to the container that add new or modify existing data are stored in this writable layer. When the container is deleted, the writable layer is also deleted. The underlying image remains unchanged.")
- Docker docs: OverlayFS storage driver ("Don't directly manipulate any files or directories within /var/lib/docker/ ."; "As with other copy-on-write filesystems, OverlayFS performs copy-up operations whenever a container writes to a file for the first time. This can add latency into the write operation, especially for large files.")
- All output on this page was produced in this session with Docker Engine 29.9.2 (containerd image store) on the test machine (Ubuntu 24.04 userland in a Firecracker microVM, custom kernel 6.18.44, cgroup v1).
What you now know
Every container gets its own writable layer, the upper directory of its overlay mount, over the image's shared read-only layers. Its writes are invisible to other containers, survive stopping and restarting, and are deleted with the container by docker rm. Data that must outlive the container doesn't belong there.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.