Part 6 of 7 · 3 min read
Layers and tools
Layers and what real tools add
Think of a stack of transparent sheets on a projector. The bottom sheet has a printed map, and each sheet above adds or covers a detail. Looking down, you see the combined picture, and the lower sheets are never changed. The analogy stops in one place: a transparency covers a detail with ink, while an overlay hides a file with a whiteout entry.
In plain words: A real image is made of layers, and a container is a read-only stack with one writable layer on top, joined by the overlay file system (Level 9). Many containers share the same lower layers, which saves space, and a change is written to the container's own upper layer (copy-on-write, Level 9). A test with a lower directory holding app.conf and base.txt, an upper directory and a merged view:
merged at start: app.conf base.txt
(then: edit app.conf, add new.txt, delete base.txt, all through the merged view)
merged afterwards: app.conf new.txt app.conf says "changed in container"
lower (unchanged): app.conf base.txt app.conf says "from image"
upper: app.conf new.txt and base.txt as a character device (the whiteout)
The lower layer was never changed, so a hundred containers can start from one image, and each pays only for its own upper layer. Throwing the container away deletes only the upper layer.
A worked example. An image of 200 MB is used by 50 containers. Without layers, 50 copies take 50 x 200 = 10,000 MB. With a shared lower layer, 200 MB plus 50 upper layers of, say, 5 MB each (made up to show the idea) takes 200 + 250 = 450 MB, which is 22 times less.
The key lines. What Docker-style tools add to the model is large and mostly practical: image formats and registries, layer building from a recipe, a network (a virtual cable pair to a bridge, with address translation), a user namespace and dropped capabilities, a seccomp filter and a MAC profile, exec into a running container, logs, and restart rules. The kernel parts are the ones of this level.
Interview angle
A common question is "What is the difference between a container and a virtual machine?" A good answer says that a VM virtualises hardware and runs its own kernel, a container shares the host kernel and uses namespaces and cgroups, so it starts faster and costs less but isolates less, and then names the extra layers (user namespace, capabilities, seccomp, policy) that harden a container.
Check yourself: 1. What is in the writable layer of a container? 2. Why do 50 containers from one image use little more space than one? 3. Name three things that Docker-style tools add to this model.
Words you will meet
| Word | Meaning |
|---|---|
| Container | A process group with its own view, limits, files and reduced rights, on a shared kernel. |
| Root file system | The tree that becomes / in the container. |
| Image / layer | A packaged root file system, stored as stacked read-only layers. |
| Overlay file system | Joins layers, with a writable one on top. |
| Whiteout | A marker that hides a lower file. |
| Shared kernel | One kernel for the host and all containers. |
Quick notes
- A container is a process with namespaces, a cgroup, a root directory and reduced rights.
- The model:
clonewith four flags,chroot,mount /proc,exec. 31 lines, 3 ms to start. - It shares the host kernel (
cat /proc/versionshows it), so a kernel flaw reaches all. - The cgroup keeps it small: the 20 MiB group killed the memory test and the host was fine.
- Layers: shared read-only lower layers, a writable upper layer, whiteouts for deletions.
- Real tools add a user namespace, dropped capabilities, seccomp, MAC, a network and images.