Sub topic 2
What an Image Layer Is
This chapter assumes you can run docker build and docker run, read a simple Dockerfile, and use a terminal. It explains what a layer is and how layers make up an image. Later chapters cover which instructions create layers and how the build cache uses them.
Layers are filesystem changes
An image layer is a stored set of filesystem changes. Each layer records which files were added, modified, or deleted compared with the layer below it. A layer does not hold a full copy of the filesystem. It holds only the difference.
An image is an ordered stack of these layers plus metadata, such as the default command and environment variables. Docker builds the final filesystem by applying the layers from bottom to top. A file in a higher layer hides a file with the same path in a lower layer.
Layers are read-only. Once a layer is created, nothing changes it. A new change always becomes a new layer on top.
Images and containers
An image is the template. A container is a running instance of it. When you start a container, Docker adds a thin writable layer on top of the image's read-only layers. Any file the container creates or edits goes into that writable layer. The image itself stays unchanged.
This is why many containers can start from one image at the same time. They share the read-only layers and each gets its own writable layer. When a container is removed, its writable layer is removed with it.
A small example
Consider this Dockerfile.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*The FROM line brings in the layers of debian:bookworm-slim. The RUN line runs a command in a temporary container. Docker then saves whatever that command changed on the filesystem as a new layer on top. In this case that is the installed curl binary and its libraries.
The image now has the base image's layers plus one more. You can see the stack after building.
docker build -t layers-demo .
docker history layers-demodocker history prints one row per step, newest first, with the size each step added. A later chapter covers this output in detail.
Layers are identified by their content
Docker names each layer by a digest. A digest is a hash computed from the layer's contents. Two layers with identical contents have the same digest, so Docker can store that layer once and reuse it. Changing a single byte produces a different digest. This is what lets Docker tell whether a layer is the same as one it already has.
docker image inspect --format '{{json .RootFS.Layers}}' layers-demoThe command prints the digests of the filesystem layers, from bottom to top.
Common misunderstandings
- A layer is not a snapshot of the whole filesystem. It stores only changes. The image's total size is roughly the sum of its layers' sizes.
- Deleting a file in a later layer does not remove it from earlier layers. The later layer only marks the file as deleted. The earlier layer still stores its bytes. A later chapter shows how this inflates images.
- Not every Dockerfile line adds files. Some instructions only change metadata. The next chapter lists which ones.
Summary of the model
- An image is a stack of read-only layers plus metadata.
- Each layer stores the filesystem changes from one build step.
- Layers are identified by a digest of their contents, so identical layers can be shared.
- A container adds one writable layer on top and leaves the image untouched.
Exercise
You build an image with three layers, then start five containers from it. Each container creates a 10 MB file. How many copies of the image's three layers are stored on disk, and where do the five 10 MB files go?
Show answer
Only one copy of the three read-only layers is stored, and all five containers share it. Each 10 MB file goes into the writable layer of the container that created it, so that data totals about 50 MB across five separate writable layers.
Exercise
A layer contains a file at /app/config.txt. A higher layer contains a different file at the same path. Which version does a container see, and is the lower version deleted from the image?
Show answer
The container sees the version from the higher layer, because it hides the lower one. The lower version is not deleted. Its bytes remain stored in the lower layer.