Sub topic 8
Images and layers: how containers are packaged
This chapter assumes you can use a Linux or macOS command line, know what a process and a filesystem are, and have Docker installed. You have already seen that a container is an isolated process. This chapter covers what that process runs from: the image.
What an image is
A container image is a packaged filesystem plus metadata. The filesystem holds the program, its libraries, and its configuration files. The metadata holds the default command, environment variables, and the exposed ports. An image has no running process. A container is a process started from an image.
An image does not include a kernel. It uses the host kernel, as the shared-kernel chapter explained. This is a main reason images are small compared with VM disk images, which carry a full guest operating system.
Layers
An image is a stack of read-only layers. Each layer stores only the files that changed relative to the layer below it. Instructions in a Dockerfile that change the filesystem, such as RUN and COPY, each add a layer. Instructions such as CMD only add metadata.
FROM alpine:3.20
RUN apk add --no-cache curl
CMD ["curl", "--version"]FROM brings in the layers of the base image. RUN adds one layer that contains the files installed by apk. CMD sets the default command and adds no files.
You can list the layers of an image by their digests. A digest is a hash of the layer content, so identical content always has the same digest.
docker image inspect --format '{{json .RootFS.Layers}}' alpineThe output is a JSON list of sha256:... values, one per layer.
How a container uses layers
When Docker starts a container, it mounts the image layers together using a union filesystem (usually overlay2 on Linux). It then adds a thin writable layer on top. This gives three behaviors you should know:
- Reads fall through to the first layer that contains the file.
- Writes use copy-on-write. If a process changes a file from a lower layer, the file is first copied into the writable layer and then changed there.
- Deleting a file from a lower layer hides it with a marker in the upper layer. The bytes stay in the lower layer.
When the container is removed, its writable layer is removed too. Data that must survive belongs in a volume, not in the container filesystem.
Why layers matter
Layers are shared. Two containers started from the same image use the same read-only layers on disk and add only their own writable layer. Two different images built on alpine:3.20 store that base once. Layers are also cached during builds and transferred individually when you pull an image, so Docker downloads only the layers you do not already have.
Common errors
- Deleting a file in a later layer does not shrink the image. If one
RUNdownloads a large archive and a laterRUNdeletes it, the archive still sits in the earlier layer. Download, use, and delete in the sameRUNinstruction. - Putting frequently changing instructions early. A changed layer invalidates the cache for every layer after it. Put stable steps, such as installing dependencies, before steps that copy your changing source code.
- Storing data in the writable layer. It is lost when the container is removed and it is slower than a volume.
- Treating a tag as a fixed version. A tag such as
alpine:3.20can be moved to a new image. The digest identifies exact content.
Exercise
A Dockerfile has RUN wget https://example.com/big.tar.gz followed by a separate line RUN rm big.tar.gz. Why is the final image still large, and how do you fix it?
Show answer
Each RUN creates its own layer. The first layer stores the downloaded archive, and the second layer only hides it with a deletion marker, so the bytes remain in the image. Combine the commands into one RUN (for example, download, extract what you need, and remove the archive in the same instruction) so the archive never exists in a committed layer.
Exercise
You start three containers from the same 200 MB image. Roughly how much disk space do the image layers use, and why?
Show answer
About 200 MB in total, not 600 MB. All three containers share the same read-only layers. Each container adds only its own thin writable layer, which holds only the files it changes.