In this lesson, you will learn how union filesystems enable the creation of a single, coherent directory tree from multiple distinct source directories.
The Theory of Merging
At the heart of containerization lies a paradox: we want each container to have its own unique filesystem (including a base OS, libraries, and binaries), yet we don't want to duplicate hundreds of megabytes of identical data for every container we launch.
Union filesystems solve this by implementing a layered architecture. Instead of a single static block device, the kernel presents a "merged" view of multiple read-only layers stacked beneath a single, writable "upper" layer. When a process accesses a file, the union filesystem looks for it in the top layer; if it doesn't exist, it traverses downward through the stack until it finds the file in a lower layer.
The "Copy-on-Write" Mechanism
The genius of this approach is the Copy-on-Write (CoW) mechanism. Imagine you have a base layer containing the /usr/bin/python binary. If a containerized process needs to modify that binary, the filesystem does not change the read-only base. Instead, it copies the file up to the writable top layer first, then performs the modification there. The original file remains untouched for all other containers sharing that base.
Consider this conceptual structure:
/merged-view
├── /app # Writable top layer
├── /bin # Read-only layer (layer 2)
└── /lib # Read-only base (layer 1)
If you list /merged-view/bin, the kernel transparently aggregates the contents of the /bin folder from all layers, masking lower-level files if a higher-level layer provides an identical filename (a process called "shadowing"). This allows developers to distribute base images (like Debian or Alpine) once, and create thousands of thin, specialized containers on top of them without consuming additional storage for the base system.
By decoupling the read-only base from the ephemeral state of the container, we gain the ability to spin up "immutable infrastructure" that is both highly efficient and trivial to discard.
Key Takeaway: Union filesystems provide a unified directory view by stacking layers and using Copy-on-Write to isolate changes, enabling efficient image sharing and container state management.