Sub topic 2
What an image layer is
This chapter explains what a layer is, how layers stack into an image, and where a running container's changes go. It assumes you can run docker build, docker run, and docker pull, and that you can read a simple Dockerfile. Later chapters cover the details this chapter only mentions: which instructions create layers, content addressing, and copy-on-write.
The core idea: a layer is a set of filesystem changes
A Docker image is not one big disk image. It is an ordered list of layers. Each layer records what changed in the filesystem compared with the layer below it.
A layer can contain three kinds of change:
- Files and directories that were added.
- Files that were modified. The layer holds the whole new version of the file.
- Files that were deleted. The layer holds a marker, not the file.
In the OCI image format, which Docker images follow, each layer is stored as a tar archive of those changes. The first layer is usually a complete base filesystem, for example the root filesystem of Debian or Alpine. Every later layer holds only the difference from the layers before it.
When Docker needs a filesystem, it applies the layers in order. A file in a higher layer hides a file with the same path in a lower layer. The result is one merged view, which is what the container sees as /.
Images and the container layer
Layers in an image are read-only. Once a layer exists, nothing changes it. Docker identifies it by a digest, a fingerprint of its contents, so a modified layer would be a different layer. Digests are the subject of a later chapter.
When you run docker run, Docker does not copy the image. It stacks one new, empty, writable layer on top of the image's layers. Everything the container writes goes into that layer: new files, edited files, and deletions.
You can see this with docker diff, which lists the changes in a container's writable layer.
docker run --name scratchpad alpine:3.20 sh -c 'echo hi > /hello.txt'
docker diff scratchpad
docker rm scratchpadThe docker diff output contains a line like A /hello.txt. A means added, C means changed, and D means deleted. After docker rm scratchpad, the writable layer is deleted and the change is gone. The image is the same as before.
Two consequences follow:
- Many containers can start from the same image and share its layers. Each container pays only for its own writable layer.
- Data written inside a container does not outlive it. To keep data, use a volume or a bind mount, which are outside the layer stack.
How a build produces layers
docker build starts from the layers of the base image named in FROM. For each instruction that changes the filesystem, it does the following:
- Starts a temporary container from the layers built so far.
- Runs the instruction inside it.
- Records the files that changed as a new layer.
- Adds that layer to the top of the stack.
Consider this Dockerfile:
FROM alpine:3.20
RUN echo "one" > /one.txt
RUN echo "two" > /two.txt
RUN rm /one.txtThe base image alpine:3.20 is a single layer. Each RUN adds one more, so the final image has four:
| Layer | Source | Contents |
|---|---|---|
| 1 | `FROM alpine:3.20` | The Alpine root filesystem |
| 2 | first `RUN` | `/one.txt` added |
| 3 | second `RUN` | `/two.txt` added |
| 4 | third `RUN` | A deletion marker for `/one.txt` |
You can check the count after building the image:
docker build -t layer-demo .
docker image inspect --format '{{len .RootFS.Layers}}' layer-demoThe second command should print 4. Inspecting layers more closely is a topic for a later chapter. Here the count confirms the model: one filesystem-changing instruction, one layer.
What a deletion really is
Layer 4 above does not remove /one.txt from storage. Layer 2 is read-only and still contains the file. Layer 4 holds a whiteout, a special marker file named .wh.one.txt in the OCI format. When Docker merges the layers, the whiteout hides the file from the container's view.
The container cannot see /one.txt, but the bytes are still stored in layer 2. The image is still as large as it was before the rm, plus a few bytes for the marker. Later chapters return to this because it is a common cause of bloated images. For now, remember the rule: a layer can hide data from lower layers, but it can never erase it.
Why the design uses layers
Splitting an image into layers gives Docker several advantages:
- Sharing on disk. If two images start from the same base, Docker stores the base layers once. Ten images built on
debian:bookworm-slimuse one copy of the Debian layer. - Smaller transfers. When you pull an image, Docker downloads only the layers you do not already have. Pushing works the same way: the registry skips layers it already holds.
- Reuse during builds. If an instruction and everything beneath it have not changed, Docker can reuse the layer it built last time instead of running the instruction again. This is the build cache, and the next chapters build on it.
- Cheap container start. Starting a container adds one empty layer instead of copying a filesystem.
The design also has costs. Each layer adds some overhead, and a big file that appears in any layer stays in the image forever. How you write the Dockerfile determines how large the image gets and how well the cache works.
Layers and metadata
Not everything in an image is a layer. An image also carries configuration: the default command, environment variables, exposed ports, the working directory, and the history of how it was built. This configuration is stored as metadata, separate from the filesystem layers.
Some Dockerfile instructions change only metadata and add no files. ENV, CMD, EXPOSE, and WORKDIR (when the directory already exists) are examples. They appear in the image's history but contribute no filesystem layer. Which instructions create layers is the topic of the next chapter, so treat this as a preview.
Common misunderstandings
- "Each layer is a full copy of the filesystem." It is not. Only the first layer is a full filesystem. Every later layer holds just the changes.
- "A smaller
RUNcommand makes a smaller layer." The layer size depends on the files the command changes on disk, not on the length of the command. A shortRUNthat downloads a large file creates a large layer. - "
rmin a later step reclaims space." As shown above, it hides files without freeing the layer that stores them. - "Changes in a container change the image." They do not. The container's writable layer is separate. To make a change part of an image, you build a new image, for example with a Dockerfile.
- "Layers are ordered by importance." Order is the only relationship that matters. A layer depends on everything below it, because its changes were recorded against that exact state of the filesystem.
Summary of the model
An image is an ordered stack of read-only layers, and each layer is a set of file changes relative to the one below. The merged view of the stack is the filesystem a container sees. A running container adds a writable layer, which is deleted with the container. A build creates one new layer for each filesystem-changing instruction. Deletions are recorded as markers, so they never reduce image size.
Exercise
A Dockerfile contains FROM debian:bookworm-slim, then RUN curl -fsSLO https://example.com/big.tar.gz (a 500 MB download), then RUN rm big.tar.gz. A teammate says the final image no longer contains the 500 MB archive, so it must be small. Is the teammate right? Explain using layers.
Show answer
No. The curl step creates a layer that stores the 500 MB archive. That layer is read-only. The rm step creates a new layer that holds only a whiteout marker for big.tar.gz. The container no longer sees the file, but the earlier layer is still part of the image, so the image is still about 500 MB larger than the base. To avoid this, download, use, and delete the file in a single RUN instruction, so that no layer ever stores it.
Exercise
You start three containers from the same image. Each one writes a 10 MB log file while running. Roughly how much additional disk space do the three containers use, and what happens to the data when you remove them with docker rm?
Show answer
About 30 MB in total, 10 MB in each container's own writable layer. The image layers are shared, so they are not duplicated. When you run docker rm, each writable layer is deleted along with its log file. To keep the logs, write them to a volume or bind mount.