The issue opens pre-filled with this page title and URL.
A container image is a filesystem split into layers, with a configuration that says which program to run. The container you built by hand started from a folder used as its root: the image is that folder, prepared to be stored, transferred and reused. This page explains layers, the configuration, the name of a layer computed from its content, and how the kernel stacks layers.
What you will learn
Describe an image as layers of files and a configuration
Explain why a layer takes the fingerprint of its content as its name
Explain how several layers become a single filesystem
Predict where a change made from a container goes
Prerequisites
You have followed the previous module, in particular A container by hand with unshare and nsenter: you know that a container is an isolated process that sees a folder as its root /.
No tool to install: this page runs no command. Docker, presented in the Docker guide, is installed at level 2.
What is a container image made of?
An image contains two things: the files of the container, stored in layers, and a configuration. Read the diagram from bottom to top: layer 1 brings the base system, layer 2 the libraries, layer 3 the application code. The configuration, at the top, is not a layer: it contains no file.
Each layer holds only the differences with the one below it (files added, modified or deleted), in a tar archive like the one that gave the root folder of your container. The configuration gives the command to run, the environment variables, the user and the declared ports.
A layer never changes after it is created. To delete a file from a lower layer, the layer above adds a deletion marker (a file named .wh. followed by the name, in the OCI specification): the file disappears from the overall view, but its bytes remain in the original layer.
Why is the name of a layer computed from its content?
A layer is named by the SHA-256 fingerprint of its content, its digest: sha256: followed by 64 hexadecimal characters. Two identical layers have the same name, and a layer that differs by one byte has another one. This is content-addressable storage, with three consequences.
Deduplication: a layer shared by ten images is stored only once.
Minimal transfer: only the layers missing from the machine are downloaded.
Verification: recomputing the fingerprint of a layer proves that its content has not been altered.
The manifest is the document of the image that lists its layers. For each one, it records the digest of the object as it is stored and transferred, often a compressed archive. The configuration records the diff_id, the fingerprint of the same archive once uncompressed. A single layer can therefore carry two fingerprints, depending on whether you look at the stored object or at its uncompressed content.
How does the container see the layers as a single filesystem?
The Linux kernel stacks the layers of the image with a layered filesystem, most often OverlayFS, then adds a writable layer specific to the container on top. The container sees the result at its root /: a single tree of files. When two layers contain the same path, the one above hides the other.
In the diagram, the layers of the image are at the bottom, read-only. The writable layer, highlighted, receives what the container creates or modifies. The container view, at the top, is not one more layer: it is the whole stack seen as a single filesystem.
Cross-section of a container: at the bottom, two read-only layers of the image, the base layer and an added layer; above them, highlighted, the writable layer of the container, which receives the files created or modified and is deleted with it; at the very top, the container view, what it sees at the root /, which is not one more layer but all the layers seen as one.Container viewwhat it sees at the root /Container write layerfiles created or changed, removed with itLayer added by the imageBase layerImage layers, read-only
Reading a file is done directly in the layer that contains it. Modifying a file of a read-only layer triggers copy-on-write: the file is first copied into the writable layer, the change is applied to that copy, and the original stays intact.
Several containers started from the same image therefore share its layers and each has its own writable layer. What a container wrote disappears with it, unless it wrote it in a volume.
Your turnTwo containers start from the same image. The first one rewrites the file /etc/motd that the image contains,…
Two containers start from the same image. The first one rewrites the file /etc/motd that the image contains, then the second one reads that same file. What does the second container see, and where is the change of the first one stored?
A hint
Look for the layer in which the write happens, and whether that layer is shared between the two containers.
I have tried: show the answer
The second container sees the original file. The change was made on a copy of /etc/motd, placed in the writable layer of the first container. The layers of the image, which are shared, have not changed, and the copy disappears with the first container.
An image is a set of read-only layers, plus a configuration that is not a layer.
A layer holds only the differences with the one below it and never changes.
The name of a layer is the SHA-256 fingerprint of its content, hence deduplication, minimal transfer and verification.
The kernel stacks the layers, most often with OverlayFS, and the container sees a single tree of files.
A change made by a container goes into its writable layer, through copy-on-write, and disappears with it.
Test your knowledge
This quiz asks ten questions drawn from fourteen. It checks that you can describe the layers and the configuration of an image, explain the name of a layer and predict where a change made by a container goes.
Knowledge check
Check what you have learned with this interactive quiz
10 questions
10 min
70% to pass
How it works
The timer starts when you click Start
Multiple choice, true/false and short answer questions
You can move between questions
Detailed results are shown at the end
Starts the quiz and the timer
⏱️--:--
0 / 10
Review
(0/0)
--:--
0%
Result saved on this device. It will be sent to your account at the next sync. Sync now
0Correct
0To review
0:00Time
-Level
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.