TL;DR
A container image is a sealed package: a stack of read-only layers of files, plus a short settings record that says which program to start. A container runtime turns an image into a running container by stacking a thin writable layer on top and starting the program in isolation. Every layer is named by a hash of its contents, so identical layers can be stored once and a copy can be checked against its name.
Builds on: Linux containers, layered file systems · Leads to: packaging and deployment of services
Where it's used
Shipping a web service. A team builds one image with its code, its runtime and its libraries, then runs that same image on a laptop, a test server and a production cluster. The image contains everything except the kernel of the Operating System underneath, so the program behaves the same wherever the container runs.
Sharing a base. Many images start from the same base layer, such as a small Linux userland. A host that runs twenty services built on that base stores the base once. Each service adds only its own layers, which is why a registry of images can be far smaller than the sum of the images' sizes.
Rolling back. Because an image is identified by its contents, a deployment can name an exact image by its hash. Going back to yesterday's release means starting yesterday's image again, not rebuilding from version-controlled code that may have changed since.
How it works
Trace a tiny image with two layers. The labels below, such as h1 and h2, stand in for real hashes; a real hash is a long string produced by a cryptographic hash function.
- Build the base layer. Put a few files, say a small shell and a set of libraries, into a folder. Hash the whole folder's contents to get a name,
h1. Check: change one byte in any file and the name changes. - Build the application layer. Put the program's own files in a second folder and hash it to get
h2. This layer lists only what the application added on top of the base. - Write the manifest. The image is a small record that lists the layers in order,
h1thenh2, and a settings record: the start command, the environment variables and so on. The manifest is itself hashed to give the image its name, so the image's names form a tree of hashes. - Run a container. The runtime reads the manifest and checks which layers it already has on the host. It downloads only the missing ones over the network (HTTP), stacks the layers in order as in the layered file system, adds a writable top layer, and starts the start command as a Process (Running Program) inside Linux container isolation. Check: the running program sees one tree of files, but the base and application layers are read-only.
- Change the application. Edit the program and rebuild. Only the application layer changes, so it gets a new name
h2'. The baseh1keeps its name and is not downloaded again. Check: a new manifest listsh1andh2', and the host needs onlyh2'.
Each step can be drawn as a flow:
flowchart LR A[Files of base] -->|hash| B[Layer h1] C[Files of app] -->|hash| D[Layer h2] B --> E[Manifest: h1, h2, settings] D --> E E -->|hash| F[Image name] E -->|runtime checks which layers it has| G[Running container]
The math
Let an image consist of a manifest with a settings record and an ordered list of layers . Let be a cryptographic hash function and let be the number of bytes in a layer.
Each layer is named by its digest . The image is named by , where records the digests of its layers in order and the settings .
Step 1 in symbols
The digest depends on every byte of layer . Changing one byte of a file changes the contents and therefore the digest, with overwhelming likelihood, because a cryptographic hash is designed so that different inputs almost never give the same output. This is the reason a name can stand in for the contents.
Step 4 in symbols: which layers to download
Let be the set of digests already on the host. The runtime downloads exactly those layers with . The number of bytes downloaded is
Step 5 of the walkthrough is this formula with holding : only is downloaded.
Storage for many images
If a host runs images , the bytes stored are the sum of the sizes of the distinct layers across them, not the sum over images. Let , then the storage is . Shared layers appear once in .
Worked examples
A base layer has two files, a and b. Its digest is . Someone edits b by changing one byte. What happens to the digest of the base, and to the digest of the image built on it?
- The base contents changed, so becomes a new value, call it .
- The manifest lists the base's digest, so the manifest changes, and the image's digest changes too.
- The application layer on top is unchanged, but the new image must list in its manifest, so the host downloads the new base.
Interpretation: a one-byte change to a low layer invalidates every image built on it. Check: the old and new images have different names, so they are different objects.
Version 1 of an image has layers . Version 2 has layers , where only the top application layer changed. A host already holds , and . Which layers does it download for version 2, and how many bytes does it save? Use sizes of MB for , MB for , MB for and MB for .
- The digest set on the host is .
- Version 2 lists , and . Check which are missing: , , .
- Only is downloaded, which is 9 MB.
- A full download would have been MB, so the host saves MB.
Interpretation: the layer order puts the most-shared layers at the bottom, so updates touch only the top, and the host downloads the small part.
A host keeps the base (100 MB), a runtime layer (30 MB), and ten old application versions, each 8 MB and built on the same two lower layers. This is a hypothetical example with round sizes. How much storage do the images use, and what is the storage if the host kept the ten versions as ten separate, fully copied images?
- Shared storage: the distinct layers are the base (100 MB), the runtime (30 MB) and ten application layers (ten times 8 MB, which is 80 MB). The total is MB.
- Fully copied: each image holds its own base and runtime, so each takes MB. Ten images take MB.
- Compare: is about times more.
Try it: the host now keeps twenty application versions instead of ten, still each 8 MB, on the same base and runtime. What is the shared storage? Answer: MB.
Common mistakes and tradeoffs
It is tempting to think a container image is a virtual machine disk with its own operating system kernel. In fact the image holds files and settings only. The container uses the host's kernel and is isolated by the kernel features in Linux containers.
It is tempting to think the tag latest names a fixed version. In fact a tag is a label that someone can move to a new image at any time. The digest of an image does not move, so a deployment that must be exact should name the digest.
It is tempting to think deleting a secret from a later layer removes it from the image. In fact the earlier layer that held the secret is still in the stack, and the layer's history can reveal it. Secrets belong in run-time configuration, not in layers.
Images suit software that must run the same way in many places, that shares a base across many services, and that needs rollback by exact name. They suit less well for large data that changes often, for very deep stacks that slow lookups, and for images that must be small and carry no history. Alternatives include mounting a separate volume for data and building in stages so that build tools stay out of the final image.
History
Isolating a process from the rest of the system began with the chroot system call in Unix, which changed a process's view of the root folder. Later systems such as FreeBSD jails and Linux namespaces and control groups added more isolation. Docker, released in 2013, packaged a layered file system and a settings record into one format that many people could share. In 2015 the Open Container Initiative began standardising the image format and the runtime so that different tools could read the same images. The idea caught on because it made packaging and sharing software as simple as copying one file name.
Recap
- A container image is a read-only stack of layers plus a settings record.
- Each layer and the image are named by hashes of their contents, so identical layers are stored once and can be verified.
- A runtime downloads only the layers the host lacks and runs the image as a container with a writable top layer.
- Updating an application changes only its top layers, which keeps downloads small.
- Read next: the lessons on Linux containers and layered file systems, which this lesson combines.
Check yourself
QuestionIn one sentence, what is a container image, and what does the runtime add to it to make a running container?Show answer
A container image is a read-only stack of file layers and a settings record that names the start command. The runtime adds a writable top layer and starts the program inside container isolation on the host's kernel.
QuestionA host holds layers h1 and h2 and an image lists h1, h2 and h3, where h3 is 12 MB. How many megabytes does the runtime download to run this image?Show answer
It downloads 12 MB. Only h3 is missing from the host, and the runtime downloads just the missing layers.
QuestionWhy does changing one byte in a base layer make every image built on it a new image?Show answer
The base layer's digest is a hash of its contents, so one changed byte gives a different digest. Each image's manifest lists the base's digest, so the manifest and therefore the image's digest also change.
QuestionA team deletes a file containing an API key in a new layer. Is the key still recoverable from the image, and why?Show answer
Yes, it can be recoverable. The earlier layer that held the key is still part of the image, and deleting the file in a later layer only hides it in the combined view. The key should be moved to run-time configuration and rotated.