Union Filesystem (OverlayFS)
OverlayFS is the Linux union filesystem Docker uses (as the overlay2 storage driver) to stack read-only image layers under a writable container layer. It merges them into one view using copy-on-write.
How layers become a filesystem
OverlayFS mounts several directories as one. Image layers are the read-only lowerdir stack, the container's writable layer is the upperdir, and the combined view is the merged directory the container sees. Containers from one image share the lower layers and store only their own changes.
Copy-on-write and whiteouts
When a container changes a file from a lower layer, OverlayFS first copies the whole file up to the writable layer, then modifies it there. Lower layers are untouched, and other containers still see the original. Deleting a file creates a whiteout marker in the upper layer. The original bytes stay in the lower layer, so deleting files in a later Dockerfile instruction does not shrink the image.
Look at it on disk
ls /var/lib/docker/overlay2/docker inspect payment-api --format '{{json .GraphDriver.Data}}' | jqdocker system df -vStorage efficiency
Three containers from a 200MB image use about 200MB for the shared layers plus small writable layers, not 600MB.
Performance
Reads run at near-native speed. First writes to large existing files pay a copy-up cost, and heavy write workloads suffer. Put databases and write-heavy paths on volumes, which bypass OverlayFS.
RememberDeleting a file in a later layer hides it but does not remove it from the image. Clean up within the same
RUN, or use multi-stage builds.
Common MistakeStoring database files in the container filesystem. It is slower and is deleted with the container.
Frequently Asked Questions
Why does Docker use a union filesystem?
Copying a full filesystem per container would waste disk and startup time. OverlayFS shares read-only image layers and gives each container a thin writable layer, so storage and start time stay small.
What is copy-on-write in this context?
The first time a container modifies a file from an image layer, the file is copied up to the writable layer and changed there. The image layer stays unchanged, which keeps other containers unaffected.
Why are databases slow on the container filesystem?
Writes pass through the overlay layer with copy-up overhead and extra metadata operations. Mounting a volume for the data directory avoids the overlay and uses the native filesystem.