Skip to content
Future Site
All news
Tutorial

Episode 2 of the series Docker: Internals to Production

Docker images in depth: layers, manifests and the build cache

6 min read

Thumbnail: 'Inside a Docker image', Docker: Internals to Production episode 2, with a slide comparing a slow and a fast Dockerfile

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?

Docker images in depth: layers, manifests and the build cache. The player loads from youtube-nocookie.com when you press play.

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

The four pieces of an image: index, manifest, config and layers

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

The three questions BuildKit asks before reusing a layer

Since Docker Engine 23, builds run on BuildKit. For every step it asks three questions, in order:

  1. Was the step before this one a cache hit?
  2. Is this instruction exactly the same text as last time?
  3. For COPY and ADD: 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

A slow Dockerfile next to a fast one: copy requirements first, code last

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 history shows one row per build step with its size. WORKDIR, USER and CMD take 0 bytes: they only change the config.
  • Build again with no changes and every step says CACHED.
  • Touch one file and only its COPY and the steps after it rerun.
  • imagetools inspect --raw prints the real manifest JSON from the registry.

Practice

  1. What are the four kinds of things docker pull downloads?
  2. Why can two images share a layer without storing it twice?
  3. You changed only app.py, but pip install ran again. What is wrong with the Dockerfile?
  4. Your RUN pip install requests layer is cached. Do you get the newest version of requests?
  5. 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.