Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Container images: layers and content

By updated

Improve this page

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

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.

A container image: at the top, the configuration (start command, environment variables, user), which is not a layer; below it, three read-only layers of files, from bottom to top: layer 1 the base system, layer 2 the libraries, layer 3 the application code.Configuration, which is not a layerstart command, environment variables, userLayer 3: the application codeadded filesLayer 2: the librariesadded filesLayer 1: the base systemsystem programs and librariesFile layers, read-only
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

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.

Understand OCI explains these two fingerprints, and the lesson Read an OCI image and verify its digest has you recompute them.

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
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

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 turn

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.

Which lessons follow this page?

Understand OCI gives the vocabulary of the standard, then Read an OCI image and verify its digest opens an image on disk. Building an image and the build cache come with Docker, at level 2: Write a Dockerfile and control its build context then Organize Dockerfile layers and use the build cache.

Key takeaways

  1. An image is a set of read-only layers, plus a configuration that is not a layer.
  2. A layer holds only the differences with the one below it and never changes.
  3. The name of a layer is the SHA-256 fingerprint of its content, hence deduplication, minimal transfer and verification.
  4. The kernel stacks the layers, most often with OverlayFS, and the container sees a single tree of files.
  5. 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

FAQ: frequently asked questions about container images

These short answers pick up the opening questions: image, container, layer and copy-on-write.