In this lesson, you will learn how Union File Systems (UnionFS) stack multiple directory branches to create a single, seamless filesystem view for your containers.
The Problem of Redundancy
If you have 10 containers that all use the same base operating system (like Ubuntu), you wouldn't want 10 separate, heavy copies of the entire Ubuntu filesystem on your disk. It would be slow to create and waste massive amounts of storage.
The UnionFS Solution
A Union File System solves this by "stacking" directories (called branches) on top of each other. Think of it like a stack of transparent plastic sheets.
- Base Layers (Read-Only): The bottom layers contain the fundamental files of the OS. Because they are read-only, every container on your machine can point to the exact same physical files on the disk without conflict.
- Top Layer (Read-Write): When a container starts, UnionFS places a thin, writable layer on top of the stack. If the container needs to modify a file, it uses a technique called Copy-on-Write (CoW).
How Copy-on-Write Works
When a process inside a container wants to edit a file that exists in a read-only base layer, the system doesn't modify the original file. Instead, it copies that file "up" into the top read-write layer and applies the changes there. The original file remains untouched for everyone else.
To the container’s applications, this feels like one normal, standard filesystem. They have no idea that the "directory" they are looking at is actually a collage of different folders merged together by the storage driver.
Imagine this simplified structure:
/upper(The writable layer:config.txtupdated here)/lower2(Application layer:app.py,libs/)/lower1(Base OS layer:bin/,etc/)
When you list the files in the container, UnionFS merges these into a single view:
/ (Merged View)
├── bin/ (from /lower1)
├── etc/ (from /lower1)
├── app.py (from /lower2)
└── config.txt (modified version from /upper)
Key Takeaway: UnionFS allows containers to share immutable base layers while maintaining a unique, writable space, making containers lightweight and incredibly fast to launch.