---
title: "Docker Storage Drivers and Union Filesystems: An In-Depth Deep Dive"
date: "2026-11-17T10:00:00+07:00"
description: "Deep dive into Docker storage drivers, Overlay2 lowerdir/upperdir/merged architecture, Copy-on-Write (CoW) mechanics, and performance trade-offs."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/storage-drivers-union-filesystems-deep-dive.jpg"
alt: "Architecture diagram showing Overlay2 lowerdir, upperdir, workdir, and merged unified filesystem mounts."
tags: ['docker', 'containers', 'storage', 'linux', 'kernel', 'performance']
---

Docker relies on **storage drivers** and **union filesystems** to combine multiple read-only image layers into a single, unified view for containerized processes. 

While Docker volumes bypass the storage driver for direct I/O performance, all internal container file modifications rely on Copy-on-Write (CoW) operations managed by storage drivers like **Overlay2**, **Btrfs**, or **ZFS**. Understanding union filesystem internals is essential for diagnosing container disk latency, memory overhead, and storage leaks.

---

## 1. Union Filesystems and the Copy-on-Write (CoW) Paradigm

A **Union Filesystem (UnionFS)** allows files and directories from separate file systems (known as branches or layers) to be transparently overlaid, creating a single coherent directory tree.

### Copy-on-Write (CoW) Mechanics

1. **Read Operations:** If a process opens a file for reading, the storage driver searches the layer stack top-to-bottom. It serves the file from the topmost layer where the file exists.
2. **Write/Modify Operations:** When a process modifies an existing file residing in a lower read-only layer:
   - The storage driver performs a **Copy-on-Write (CoW)** operation.
   - The target file is copied from the read-only image layer into the container's writable layer.
   - The modification is applied to the copy in the writable layer.
   - The file in the read-only layer remains completely untouched.
3. **Delete Operations:** When a file in a read-only layer is deleted inside a container, a **whiteout file** (or opaque directory attribute) is created in the writable layer to mask the lower file from view.

---

## 2. Overlay2: The De Facto Linux Storage Driver

On modern Linux kernels, **Overlay2** is the default and recommended storage driver. Overlay2 maps directly to the native Linux kernel `overlay` filesystem module.

```
                  Unified Container View: /merged
+-----------------------------------------------------------------+
| app.py (v2)      | config.json      | database.db (whiteout)    |
+-----------------------------------------------------------------+
                                |
       +------------------------+------------------------+
       |                                                 |
       v                                                 v
Writable Container Layer: /upperdir               Read-Only Image Layers: /lowerdir
+---------------------------------+               +---------------------------------+
| app.py (v2) [Modified File]     |               | app.py (v1) [Original Base File]|
| database.db [Whiteout Marker]   |               | config.json [Base File]         |
+---------------------------------+               +---------------------------------+
```

### The Four Directories of Overlay2

When a container runs under Overlay2, the Linux kernel mounts four specific directory structures:

- **`lowerdir`:** The stacked read-only image layers separated by colons (`/layer3:/layer2:/layer1`).
- **`upperdir`:** The container's read-write layer containing all modified, newly created, or whiteout files.
- **`workdir`:** An internal state directory used by the kernel for atomic operations (such as copy-up and whiteout preparation).
- **`merged`:** The unified mount point representing the container's active root filesystem (`/`).

### Inspecting Overlay2 Mounts

You can observe active Overlay2 mount parameters directly via `docker inspect` or host mount tables:

```bash
docker run -d --name overlay-demo alpine sleep 3600
docker inspect --format='{{json .GraphDriver.Data}}' overlay-demo | jq .
```

*Output snippet:*
```json
{
  "LowerDir": "/var/lib/docker/overlay2/3a1b.../diff:/var/lib/docker/overlay2/8f2c.../diff",
  "MergedDir": "/var/lib/docker/overlay2/9e4d.../merged",
  "UpperDir": "/var/lib/docker/overlay2/9e4d.../diff",
  "WorkDir": "/var/lib/docker/overlay2/9e4d.../work"
}
```

You can also view the kernel overlay mount in the host filesystem table:

```bash
mount -t overlay
```

---

## 3. Storage Driver Comparison Matrix

| Storage Driver | File vs. Block CoW | Page Cache Sharing | Common Use Cases / Characteristics |
| :--- | :--- | :--- | :--- |
| **Overlay2** | File-level | **Yes** (Multiple containers share host page cache) | Default for Linux. Extremely memory-efficient, low latency. |
| **Btrfs** | Block-level (Subvolume) | No | Native driver for Btrfs root filesystems. Fast snapshotting. |
| **ZFS** | Block-level (Dataset) | No (Uses ZFS ARC cache) | Enterprise storage servers, high dataset density. |
| **DeviceMapper** | Block-level (thin pool) | No | **Deprecated.** Historical driver for RHEL/CentOS 7 without Overlay2 support. |

---

## 4. Performance Trade-Offs and Copy-Up Overhead

While Overlay2 provides outstanding read performance and page cache sharing across containers, file-level Copy-on-Write introduces performance penalties under specific I/O patterns:

### The "Copy-Up" Latency Penalty

If an application opens a 10 GB log file residing in `lowerdir` to append a single line:
1. Overlay2 must copy the entire **10 GB file** from `lowerdir` to `upperdir` before writing the first byte.
2. This creates severe I/O blocking and consumes equivalent space in the container's writable layer.

### Mitigation: Use Docker Volumes for Heavy Writes

For heavy write workloads (relational databases, message queues, log collectors), **always bypass the storage driver** using Docker Volumes or bind mounts:

```bash
# Correct approach for write-heavy workloads
docker run -d \
  --name postgres-db \
  -v pgdata:/var/lib/postgresql/data \
  postgres:16
```

Named volumes write directly to host storage paths without passing through the storage driver or Copy-on-Write mechanics.

---

## Key Takeaway

**Takeaway:** Storage drivers like **Overlay2** combine read-only image layers (`lowerdir`) with writable container layers (`upperdir`) via kernel union mounts (`merged`). While Copy-on-Write provides fast container instantiation and page cache sharing across containers, heavy file write workloads should always use **Docker Volumes** to avoid the I/O latency penalty of file copy-ups.

---

## Sources

- [Docker Documentation: Select a storage driver](https://docs.docker.com/storage/storagedriver/select-storage-driver/)
- [Docker Documentation: Use the OverlayFS storage driver](https://docs.docker.com/storage/storagedriver/overlayfs-driver/)
- [Linux Kernel Documentation: Overlay Filesystem](https://docs.kernel.org/filesystems/overlayfs.html)
