Episode 2 of the series Docker: Internals to Production
Docker images in depth: layers, manifests and the build cache
6 min read
In the first episode we opened up a running container and found an ordinary Linux process. This time we open up the thing it starts from: the image. Once you know what is inside, two everyday questions answer themselves: why is my build so slow, and why is my image so big?

Not a disk image
Many people picture an image as one big virtual disk, like an ISO file. It isn't.
An image is an ordered stack of compressed tarballs, plus a JSON manifest that lists their hashes and a config that says how to run them.
No magic, just files and JSON, defined by the Open Container Initiative (OCI) image specification. Docker Hub, GitHub Packages, AWS ECR and every other registry speak the same format.
What you download on docker pull

| Piece | What it holds |
|---|---|
| Index | One entry per platform (linux/amd64, linux/arm64); your machine picks its own |
| Manifest | The digest of the config and the ordered list of layers |
| Config | Environment variables, working directory, user, entrypoint, and the layer history |
| Layers | Gzipped tarballs: the files each build step added, changed or deleted |
Layers work like git commits
The easiest way to think about layers is git commits for a filesystem:
- each layer stores only the diff from the one below it;
- it is named by the SHA-256 of its content (content-addressable storage);
- layers stack in order, from the base up;
- two images built on the same base share those exact bytes, on disk and in memory.
Ten images built on python:3.12-slim store its base layers exactly once.
A manifest, up close
Here is a trimmed real manifest. Notice that nothing is referenced by name: every piece is a digest, so if a single byte changes, the hash changes and the registry knows.
{ "schemaVersion": 2, "config": { "digest": "sha256:d5b5…4f49", "size": 7023 }, "layers": [ { "digest": "sha256:bc07…", "size": 29150000 }, { "digest": "sha256:8893…", "size": 3412000 } ] }
Two hashes per layer
Every layer has two digests, and mixing them up is a classic source of confusion:
- the distribution digest is the hash of the compressed tarball that travels over the network (it is in the manifest);
- the DiffID is the hash of the uncompressed tarball once it is unpacked (it is in the config, under
rootfs.diff_ids).
If the hashes you see in a registry don't match what docker image inspect shows, that is usually why.
How the build cache decides

Since Docker Engine 23, builds run on BuildKit. For every step it asks three questions, in order:
- Was the step before this one a cache hit?
- Is this instruction exactly the same text as last time?
- For
COPYandADD: are the source files byte for byte the same?
Three yeses and the layer is reused in zero seconds. One miss, and every step after it rebuilds.
RUN only checks the text
BuildKit compares the command string, never the network. If you install an unpinned package, the cached layer keeps whatever version it got the first time: pip install fastapi cached in March is still the March version today. Use --no-cache to force a fresh run, or better, pin your versions.
Three mistakes that cost minutes and megabytes
1. Code before dependencies

FROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY src/ ./src/ CMD ["python", "src/main.py"]
With COPY . . before pip install, changing one comment in your code invalidates the copy step, so every package is reinstalled on every build. Copy only the requirements file, install, and copy your code last: a code change now rebuilds only the last COPY.
2. Cleaning up too late
RUN apt-get update && \
apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
Remember copy-on-write from episode 1: once a layer is saved, it never changes. If you delete the package lists in a separate RUN, the first layer still contains those ~35 MB, and the second only adds a whiteout marker that hides them. They still ship with your image. Clean up in the same RUN that made the mess.
3. latest and unpinned packages
FROM python:latest moves under your feet: one day your build pulls a new operating system release and nothing in your code changed. Pin the exact version, pin the digest for production, and install from a lock file:
FROM python:3.12.7-slim@sha256:d5b5… COPY requirements.lock . RUN pip install --no-cache-dir -r requirements.lock
Same inputs, same image, every time.
See it yourself
docker image history layer-lab:v1 docker build -t layer-lab:v1 . echo "# comment" >> app.py docker build -t layer-lab:v2 . docker buildx imagetools inspect python:3.12-slim --raw
docker image historyshows one row per build step with its size.WORKDIR,USERandCMDtake 0 bytes: they only change the config.- Build again with no changes and every step says
CACHED. - Touch one file and only its
COPYand the steps after it rerun. imagetools inspect --rawprints the real manifest JSON from the registry.
Practice
- What are the four kinds of things
docker pulldownloads? - Why can two images share a layer without storing it twice?
- You changed only
app.py, butpip installran again. What is wrong with the Dockerfile? - Your
RUN pip install requestslayer is cached. Do you get the newest version of requests? - Why doesn't
RUN rm -rf /var/lib/apt/lists/*on its own line make the image smaller?
Answers: 1. An index, a manifest, a config and the layer tarballs. 2. Layers are named by the hash of their content, so identical content has the same name and is stored once. 3. The code is copied before the install step; copy the requirements file first and the code last. 4. No: BuildKit only compares the command text, so the cached layer keeps the old version until you pin it or build with --no-cache. 5. The files still exist in the earlier layer; the new layer only hides them. Delete them in the same RUN.
Next
In the next episode we write production Dockerfiles: multi-stage builds, small base images and non-root users. Follow the whole Docker: Internals to Production series.