Sub topic 1
How images are built from layers
Prerequisites
This chapter assumes you can pull, build, and run images with Docker, read a simple Dockerfile, and use a Linux command line. You do not need any tools beyond Docker itself. Later chapters introduce docker history and dive.
What a layer is
A Docker image is not one large file. It is a stack of read-only layers, and each layer stores the filesystem changes made by one build step. A layer holds files that were added, modified, or deleted compared with the layer below it.
Docker identifies each layer by a content hash. Two images that start from the same base image share the base layers on disk, so the base is stored once. This saves space and speeds up pulls.
The union filesystem
When a container runs, Docker combines all the layers into one view with a union filesystem (on most systems, OverlayFS). A union filesystem is a filesystem that merges several directories so they look like a single directory tree.
The merge follows these rules:
- If a path exists in only one layer, you see that file.
- If a path exists in several layers, the topmost layer wins.
- If an upper layer deletes a file, it records a marker called a whiteout. The file disappears from the merged view, but the original file stays in the lower layer.
The last rule matters most for image size. Deleting a file in a later layer hides it. It does not remove its bytes from the image.
Why layers add to image size
The size of an image is roughly the sum of the sizes of all its layers. Each layer contributes the bytes it adds. It never gives back bytes that a lower layer already holds.
Consider this sequence of steps:
- A layer adds a 100 MB archive.
- A later layer deletes that archive.
The merged filesystem shows no archive. The image still carries 100 MB, because the layer from step 1 is part of the image and is still stored and transferred.
The same thing happens when a later layer overwrites a file. Both the old and the new versions exist, each in its own layer. Only the new one is visible.
A layered example
The Dockerfile below is deliberately wasteful. You will analyse it in the next chapters.
FROM debian:bookworm-slim
RUN apt-get update
RUN apt-get install -y curl
COPY data.tar /tmp/data.tar
RUN tar -xf /tmp/data.tar -C /opt
RUN rm /tmp/data.tarWalk through what each step stores:
| Step | Instruction | What the layer contains |
|---|---|---|
| 1 | `FROM debian:bookworm-slim` | The base image layers, inherited as they are |
| 2 | `RUN apt-get update` | Package index files in `/var/lib/apt/lists` |
| 3 | `RUN apt-get install -y curl` | `curl`, its libraries, and package manager records |
| 4 | `COPY data.tar /tmp/data.tar` | The full archive |
| 5 | `RUN tar -xf ...` | The extracted files under `/opt` |
| 6 | `RUN rm /tmp/data.tar` | Only a whiteout marker, a few bytes |
Two kinds of waste appear here. The apt package lists from step 2 stay in the image because nothing removes them. The archive from step 4 stays in the image because step 6 only hides it. The extracted files from step 5 also exist, so the data is stored twice.
What this means in practice
Keep these working rules in mind as you read the later chapters:
- Every layer is permanent once it is built. You cannot shrink an earlier layer from a later one.
- Order and grouping of instructions affect size, not just the final contents of the filesystem.
- A small final filesystem does not guarantee a small image. The image size includes all hidden and overwritten content.
The following chapters show how to see this waste with docker history, how to find the exact files with dive, and how to fix the Dockerfile.
Exercise
A Dockerfile has COPY big.iso /tmp/big.iso (a 500 MB file) followed by RUN rm /tmp/big.iso in a separate instruction. When you run the container and list /tmp, the file is gone. Roughly how much does this pair of instructions add to the image size, and why?
Show answer
About 500 MB. The COPY layer stores the full file. The RUN rm layer only adds a whiteout marker that hides the file in the merged view. The original bytes stay in the COPY layer, so the image still carries and transfers them.
Exercise
Two images are built from the same debian:bookworm-slim base. Is the base stored twice on your disk? Explain.
Show answer
No. Layers are identified by content hash, so identical base layers are stored once and shared by both images.