arthrop0d

Brief articles on varied topics.

Overlay Filesystem Copy-Up in Containers

containerslinuxoperating systems

Overlay filesystem copy-up is the operation that copies a file from a container image’s read-only layer into the container’s writable layer before modifying it. The container still presents one filesystem to the process, but the changed copy is stored separately from the image.

The problem copy-up solves

Container images are built from layers. Each layer is an immutable filesystem snapshot, and image layers can be shared by many containers. This makes images efficient: ten containers can read the same copy of an application binary, library, or configuration file instead of storing ten copies.

But a running container needs somewhere to write. Processes may create files, update configuration, write logs, or modify a database. Making the image itself writable would break its immutability and would make sharing unsafe: one container’s change could affect every other container using that image.

An overlay filesystem provides the compromise. It combines:

The Linux filesystem implementation commonly used for this is OverlayFS. Container runtimes and storage drivers use it to make those layers appear as one directory tree.

Reading is simple; writing is not

When a process reads /etc/app/config.json, OverlayFS looks for the path in the upper layer first. If it is not there, it searches the lower image layers and returns the lower-layer file.

If the process opens that same file for writing, the lower file cannot be changed in place. OverlayFS first performs a copy-up: it copies the file into the upper layer, preserving the relevant contents and metadata such as ownership and permissions. The write then applies to the upper-layer copy.

After that, future accesses find the upper copy first:

merged view:  /etc/app/config.json
                 │
                 ├── upper layer: modified copy  ← reads and writes use this
                 └── lower layer: original image copy

The original image file remains unchanged. If the container is removed, its upper layer is removed too, and the image’s original file is still available to a new container.

Copy-up happens when a lower-layer object needs to change, not only when application data is appended. Operations such as changing ownership, permissions, or timestamps can also require an upper-layer representation.

A file that is created for the first time does not need copy-up: it is created directly in the upper layer. A file that has already been copied up also needs no second copy-up for later writes.

Directories have similar behavior. To create a new file inside a directory that exists only in a lower layer, OverlayFS may copy up the directory’s metadata and then create the new file in the upper layer. The exact details depend on the operation and filesystem options, but the important result is the same: the upper layer records the container’s version of the changed path.

Why a one-byte change can use many megabytes

Copy-up normally copies the file as a whole before the modification. A one-byte edit to a 500 MB lower-layer file can therefore require roughly 500 MB of new space in the container’s writable layer, even if the final logical change is tiny.

That storage is in addition to the image data. The lower copy may be shared and already present only once on the host, while the upper copy is charged to the individual container’s writable storage. Several containers that each modify the same large image file can each accumulate their own copy.

This is easy to encounter with files such as:

Deleting a lower-layer file also cannot remove it from the immutable image. OverlayFS records a special upper-layer marker, commonly called a whiteout, that makes the lower file appear absent in the merged view. The original lower data remains stored in the image, while the whiteout consumes a small amount of upper-layer space.

Why writes can be slower

The first write to a lower-layer file has extra work before the application’s write can complete:

  1. OverlayFS locates the file in the lower layers.
  2. It creates the upper-layer copy and copies the file’s data and metadata.
  3. It applies the requested write to that copy.

The latency and I/O cost of the first modification therefore grow with the file’s size. On a busy host, many containers performing these first writes at once can create a noticeable burst of disk activity. The copy may also consume page cache and compete with application I/O.

Later writes to that same path avoid the copy-up, so they are generally closer to ordinary writes to the upper filesystem. They are not necessarily free: the upper layer still has its own filesystem, journaling, allocation, and synchronization costs. OverlayFS is an added pathname and layering mechanism, not a guarantee of native-filesystem performance.

The effect is why incident reports and runtime messages sometimes mention overlay, copy-up, upperdir, or a container’s writable layer. A container can appear to have plenty of free image storage while its writable layer reaches its limit, or it can show a latency spike on the first update to a large file.

Designing around copy-up

For frequently changing or large data, put the data on a volume or another storage location intended to be writable. A volume is mounted into the container rather than stored in its overlay upper layer, so changing a large database file does not first copy the image’s version of that file into the container layer.

Other practical choices are to:

Copy-up is therefore the price of combining two useful properties: shared, immutable image layers and a writable container view. Reads can use the shared image data directly. The moment a lower-layer file must change, OverlayFS creates a private upper-layer version, and both the initial copy cost and the space it consumes belong to the container.

← All articles