OverlayFS Explained: The Filesystem Behind Containers and Live Images
Every container you run is an OverlayFS mount. So is a live USB that lets you install packages, and so is the writable part of an immutable distribution. It is one of the more consequential filesystems in use and almost nobody mounts it by hand.
The model
OverlayFS merges directories into one view:
lowerdir, one or more read-only layersupperdir, the writable layerworkdir, private scratch space OverlayFS needsmerged, the mountpoint where the combined result appears
Reads come from the topmost layer containing the file. Writes always go to upperdir. The lower layers are never modified.
mkdir -p /tmp/ov/{lower,upper,work,merged}
echo "from lower" > /tmp/ov/lower/a.txt
echo "also lower" > /tmp/ov/lower/b.txt
sudo mount -t overlay overlay \
-o lowerdir=/tmp/ov/lower,upperdir=/tmp/ov/upper,workdir=/tmp/ov/work \
/tmp/ov/merged
ls /tmp/ov/merged
# a.txt b.txt
# write, and watch where it lands
echo "changed" > /tmp/ov/merged/a.txt
cat /tmp/ov/lower/a.txt # from lower, unchanged
cat /tmp/ov/upper/a.txt # changed
cat /tmp/ov/merged/a.txt # changed
The lower file is intact. The merged view shows the new content because upper shadows it.
Deletion and whiteouts
The part that surprises people the first time.
rm /tmp/ov/merged/b.txt
ls /tmp/ov/merged # gone
ls /tmp/ov/lower # still there
ls -l /tmp/ov/upper/b.txt
# c--------- 1 root root 0, 0 ... b.txt
That is a whiteout: a character device with major and minor both zero. Since the lower layer cannot be modified, deletion is expressed as a marker in the upper layer saying “pretend this is not here”.
Which means deleting a file in a container does not reclaim its space. The data is still in the image layer, plus you have added a whiteout. This is the single biggest cause of container images being larger than their contents:
# wrong: the archive is still in the layer below
RUN curl -O https://example.com/big.tar.gz
RUN tar -xf big.tar.gz
RUN rm big.tar.gz
# right: one layer, nothing left behind
RUN curl -O https://example.com/big.tar.gz \
&& tar -xf big.tar.gz \
&& rm big.tar.gz
Our building container images guide covers layer hygiene properly.
Copy up
The other behaviour with real cost.
The first write to a file that exists only in a lower layer copies the whole file up first. A one-byte change to a 10 GB file writes 10 GB into the upper layer before applying your byte.
# watch it happen
ls -la /tmp/ov/upper/ # empty-ish
truncate -s 100M /tmp/ov/lower/big.bin
sudo mount -o remount /tmp/ov/merged 2>/dev/null
printf 'x' | dd of=/tmp/ov/merged/big.bin bs=1 count=1 conv=notrunc 2>/dev/null
du -sh /tmp/ov/upper/ # 100M
Consequences worth internalising:
- Databases do not belong on an overlay. Rewriting pages in place triggers copy up repeatedly. Use a volume or bind mount, as our Docker volumes guide covers
- Virtual machine images likewise
- Log files written inside a container grow the writable layer rather than the image
- Metadata-only changes such as
chmodalso trigger copy up on some configurations
This is the same class of problem as copy-on-write fragmentation on Btrfs, and the mitigations rhyme: keep the churning files off the mechanism. Our chattr guide covers the Btrfs version.
Multiple lower layers
sudo mount -t overlay overlay \
-o lowerdir=/tmp/ov/l3:/tmp/ov/l2:/tmp/ov/l1,upperdir=/tmp/ov/upper,workdir=/tmp/ov/work \
/tmp/ov/merged
Leftmost is highest priority. This is exactly how container image layers work: each instruction in a Dockerfile produces a layer, and they stack in order.
# see the layers of an image
podman image tree myimage:latest
docker history myimage:latest
# where the runtime keeps them
sudo ls /var/lib/containers/storage/overlay/ | head
sudo ls /var/lib/docker/overlay2/ | head
# what overlays are mounted right now
mount -t overlay
findmnt -t overlay
That last command on a machine running containers is worth looking at once. Every running container appears, with its lowerdir chain visible in the options.
Read-only overlays
Omit upperdir and you get a merged read-only view, which is cheap and occasionally useful:
sudo mount -t overlay overlay \
-o lowerdir=/tmp/ov/l2:/tmp/ov/l1,ro /tmp/ov/merged
The requirements that trip people up
# upper and work must be on the same filesystem as each other
df /tmp/ov/upper /tmp/ov/work
# and it must support xattrs
touch /tmp/ov/upper/t && setfattr -n user.test -v 1 /tmp/ov/upper/t && echo "xattrs ok"
The filesystem holding upperdir must support extended attributes, because OverlayFS stores its bookkeeping there. Our xattrs guide covers which filesystems do.
workdir must be empty at mount time and must be on the same filesystem as upperdir. Do not look inside it or back it up; it is internal.
Known limitations:
- NFS as
upperdirdoes not work reliably - Hard links across layers behave surprisingly
inotifydoes not see changes made directly to a lower layer, which matters for the watchers in our inotify guide- Non-standard
st_devvalues confuse software that assumes a file’s device number identifies a filesystem
Where you meet it
Containers. Every layer is a lowerdir, the container’s writable layer is the upperdir. Our Docker and Podman guide covers the runtime.
Live USBs. The ISO contains a read-only squashfs or erofs image. A tmpfs provides the upperdir, so the system appears writable and forgets everything at shutdown. Persistence is the same arrangement with the upper layer on disk instead, as our Ventoy guide and bootable USB guide touch on.
Immutable distributions. A read-only base with writable overlays for the parts that must change. Our immutable distros guide and image-based updates guide cover the model.
systemd-sysext, which overlays extension images onto /usr without modifying it.
# sysext, which is overlayfs with a systemd front end
systemd-sysext status
ls /var/lib/extensions/ 2>/dev/null
A practical use: testing a change you can undo
# overlay /etc so you can experiment and discard
sudo mkdir -p /run/etc-test/{upper,work,merged}
sudo mount -t overlay overlay \
-o lowerdir=/etc,upperdir=/run/etc-test/upper,workdir=/run/etc-test/work \
/run/etc-test/merged
# edit freely in /run/etc-test/merged, see only your changes:
sudo find /run/etc-test/upper -type f
# throw it away
sudo umount /run/etc-test/merged
Because the upper layer is on tmpfs, nothing survives a reboot, and find on the upper directory is a precise record of what you touched. That is a genuinely useful technique for working out what an installer modified.
Do not overlay a live /etc on a system you depend on. The example above overlays a copy at a separate mountpoint rather than shadowing the real one, which is the safe form.
Diagnosing a full writable layer
# how big is a container's writable layer
podman ps --size
docker ps --size
# what is in it
sudo du -sh /var/lib/docker/overlay2/*/diff 2>/dev/null | sort -rh | head
# reclaim
podman system prune -a
docker system prune -a --volumes
A container whose disk usage grows without the image changing is writing into its upper layer, and the usual culprits are logs and caches. Our disk usage guide covers finding it, and the fix is a volume rather than a bigger disk.
Frequently Asked Questions
What is OverlayFS?
A union filesystem that presents one or more read-only lower layers and a writable upper layer as a single merged directory. Reads come from the topmost layer that has the file, and writes always go to the upper layer, leaving the lower ones untouched.
What is a whiteout file?
A marker in the upper layer that hides a file present in a lower layer. Because the lower layer is read only, deleting a file cannot remove it, so OverlayFS creates a character device with major and minor zero at that path to make it appear gone in the merged view.
Why does editing a large file in a container use so much space?
Because of copy up. The first write to a file that exists only in a lower layer copies the entire file into the upper layer first, even for a one byte change. A ten gigabyte file modified by one byte costs ten gigabytes in the writable layer.
Can the upper and lower directories be on different filesystems?
The lower layers can be almost anything, including a squashfs or a read-only mount. The upper and work directories must be on the same filesystem as each other, and that filesystem must support extended attributes, which is why tmpfs and some network filesystems cause problems.
What is the workdir for?
OverlayFS needs a private directory on the same filesystem as the upper layer to stage atomic operations such as copy up and whiteout creation. It must be empty at mount time and must not be touched afterwards, since its contents are internal bookkeeping rather than anything useful.
Is OverlayFS suitable for a database?
No. Copy up behaviour means rewriting parts of large files is expensive, and the layering adds indirection to every lookup. Databases and virtual machine images belong on a volume or bind mount that bypasses the overlay entirely, which is what container runtimes recommend.