Sub topic 1
What a Docker image is
A Docker image is a read-only package that holds a filesystem and the metadata needed to start a container from it. This chapter builds the working model you will rely on for the rest of the tutorial: what an image contains, how it differs from a container, and how to inspect one.
Prerequisites
This chapter assumes you already have the following:
- Basic command-line use, including running commands and reading their output.
- Familiarity with
docker run. - A working Docker installation, either Docker Engine or Docker Desktop.
- A basic idea of what a container is: an isolated process with its own view of the filesystem, network, and process list.
What an image contains
An image has three kinds of content:
- Filesystem layers. Each layer is an archive of file changes: files added, modified, or removed. Stacked in order, the layers produce the root filesystem the container sees.
- A config. A JSON document that records defaults such as the command to run, environment variables, the working directory, exposed ports, the target CPU architecture and operating system, and the history of how each layer was made.
- A manifest. A JSON document that lists the config and the layers, identifying each by a cryptographic hash. The manifest is what a registry stores and what a client downloads first.
An image does not contain a running kernel. A container uses the kernel of the host, so the image only needs user-space files such as binaries, libraries, and configuration. This is why an image for a small static program can be only a few megabytes.
The next chapters cover layers and manifests in detail. For now, keep this picture in mind: the manifest points to a config and an ordered list of layers.
Images versus containers
An image is a template. A container is a running instance created from it. Many containers can start from one image, and none of them changes the image.
When Docker starts a container, it adds a thin writable layer on top of the read-only image layers. Anything the container writes goes into that layer. When the container is removed, the writable layer is deleted and the image is unchanged.
| Aspect | Image | Container |
|---|---|---|
| State | Immutable | Has a writable layer |
| Purpose | Template to start from | Running or stopped instance |
| Created by | `docker build` or `docker pull` | `docker run` or `docker create` |
| Removed by | `docker rmi` | `docker rm` |
| Count | One image can back many containers | Each container belongs to one image |
You can see this separation directly. The following commands start a container, write a file in it, and show that a second container from the same image does not see the file.
docker run --name first alpine sh -c 'echo hello > /note.txt && cat /note.txt'
docker run --name second alpine sh -c 'ls /note.txt'
docker diff first
docker rm first secondThe first container prints hello. The second reports that /note.txt does not exist, because each container has its own writable layer. docker diff first lists A /note.txt, which is the change recorded in the first container's writable layer.
Inspecting an image
Pull a small image and look at what Docker stores about it.
docker pull alpine:3.20
docker image ls alpine
docker image inspect alpine:3.20The inspect output is long. These two commands pick out the fields that matter most.
docker image inspect --format '{{.Id}}' alpine:3.20
docker image inspect --format '{{json .Config.Cmd}} {{.Os}}/{{.Architecture}}' alpine:3.20The first prints the image ID, a sha256: hash. The second prints the default command (["/bin/sh"] for Alpine) and the platform the image was built for, for example linux/amd64.
The image ID is the hash of the image's config. Because the config lists the layer hashes, a change to any layer changes the config and therefore the ID. This is content addressing, covered in a later chapter.
To see how an image was assembled, use docker history.
docker history alpine:3.20Each row is a step in the image's build, with the size of the layer it produced. Some steps, such as setting an environment variable, add no files and show a size of 0B.
Where images come from
An image reaches your machine in one of three ways:
- Built locally from a Dockerfile with
docker build. - Pulled from a registry, a server that stores and serves images, with
docker pull. - Loaded from a tar archive with
docker load, which is useful on machines with no network access.
The reverse operations are docker push to upload to a registry and docker save to write an archive. The rest of this tutorial covers building and registries in order.
docker save alpine:3.20 -o alpine.tar
tar -tf alpine.tar
docker load -i alpine.tarListing the archive shows the same pieces described above: a manifest file, a config file, and one directory or file per layer. The exact layout depends on the Docker version.
Common mistakes
These errors come up often when people first work with images.
- Treating a container as storage. Data written inside a container disappears when the container is removed. Use a volume for data you need to keep.
- Assuming a tag never changes. A tag such as
alpine:3.20is a movable label. The image behind it can be replaced by a newer build. Tags are covered in the naming chapter, and digests, which do not move, come later. - Confusing an image with its layers. Deleting an image with
docker rmiremoves only the layers that no other image uses. Shared layers stay. - Expecting a different kernel. A Linux image on Docker Desktop for macOS or Windows runs inside a Linux virtual machine. The image cannot bring its own kernel.
- Putting secrets in an image. Anyone who can pull the image can read every layer. A file deleted in a later layer still exists in the earlier one.
Checking your understanding
Exercise
You run docker run --name a alpine touch /x and then docker run --name b alpine ls /x. The second command fails. Why, and which part of the image or container holds the file /x after the first command?
Show answer
The file /x was written to the writable layer of container a, not to the image. The image layers are read-only and shared, so container b, which has its own empty writable layer, does not see the file. docker diff a would show A /x. Removing container a deletes the file.
Exercise
Which command prints the platform (operating system and architecture) an image was built for, and which prints the steps and layer sizes that made it?
Show answer
docker image inspect --format '{{.Os}}/{{.Architecture}}' IMAGE prints the platform, and docker history IMAGE prints the build steps with their layer sizes.