Container image layers: understand cache, deletion, and writable storage
Learn what an image layer records, how layer order and build cache work, why deleting a file may not shrink an image, and when to use a volume.
Container image layers: understand cache, deletion, and writable storage
A container image layer is an ordered set of filesystem changes. In an OCI image, a layer records files to add or modify and entries to remove relative to the layer below it. A runtime applies layers in order so they appear as one filesystem. A running container usually adds its own writable layer over the image's read-only layers; that layer is different from both image layers and persistent volumes.
What is stored in a layer?
An image manifest refers to a configuration document and an ordered list of layer blobs. OCI layers are tar-based changesets. Additions and modifications are recorded as filesystem entries; removals use special whiteout entries. Order matters: if a later layer changes or removes a path, the final filesystem view reflects the later change.
Execution settings such as environment variables and default commands belong to image configuration, not to a filesystem changeset. Therefore, not every Dockerfile instruction necessarily creates an actual filesystem layer. A metadata instruction can appear in image history without a filesystem diff.
How do layers relate to build cache?
Docker can reuse a cached build result when the instruction and its inputs match a prior build. For example, copying dependency manifests and installing dependencies before frequently changed source files can let a source-only edit reuse the earlier installation result. A changed earlier instruction or input can cause later steps to run again.
Cache reuse does not automatically check whether an external package repository has newer packages. Docker documents that a regular RUN cache match is mainly based on the command. So an unchanged RUN apt-get update may reuse its old result. Usually update package indexes and install packages in the same RUN step, and define a rebuild policy for security updates. The --no-cache option and --pull option serve different purposes: the first bypasses build cache; the second checks for a fresh base image.
Does deleting a file shrink the image?
Not necessarily. Suppose one RUN adds a large file and a later RUN deletes it. The upper layer can hide that path from the merged filesystem, but the blob in the lower image layer can remain in the image transfer. A deletion adds a change that removes the path; it does not edit the lower layer.
Clean up temporary files in the same build step that creates them, or use a multi-stage build that copies only required runtime files into the final stage. These techniques can reduce leftover build content, but the final image must still include its required runtime files, libraries, and certificates.
How is a container's writable layer different from a volume?
Files created in a running container usually go to a writable layer unique to that container. When the container is removed, that data can disappear; the layer is not a good long-term data store and is not automatically shared with another container.
Volumes persist independently of container lifetime. Bind mounts connect a host path to a container so both can access the files. If data must survive container replacement, choose a volume or bind mount suited to the application, and review ownership, backup, permissions, and host exposure.
Practical rules
Keep the build context small with .dockerignore. Order stable dependency inputs before frequently changing source to improve potential cache reuse. Do not confuse a cached package-install command with fresh package metadata. Clean temporary files in the same step or copy only final artifacts into the last stage. Use a volume or bind mount for data that must outlive a container.
Sources and scope
OCI layer changeset specification · OCI image configuration, DiffID, and ChainID · Docker image layers and cache · Cache invalidation rules · Docker container storage
Based on OCI and Docker documentation reviewed 22 November 2026. Disk layout and layer/cache reuse depend on storage driver, image format, and builder.