Sub topic 8
Why pushes are fast the second time: layers and digests
This chapter assumes you have already built an image, logged in with docker login, and pushed it once with docker push. It explains what Docker does during that push and why a second push of a similar image often finishes in seconds. You also learn the identifiers that appear in push output and when each one changes.
An image is a manifest plus blobs
A registry such as Docker Hub does not store an image as one file. It stores several pieces, each called a blob, and a small JSON document that lists them.
- Layer blobs. Each layer is a compressed tar archive holding the files that one build step added, changed, or removed.
- Config blob. This JSON file holds the image's settings, such as
CMD, environment variables, and the list of layers in order. - Manifest. This JSON document lists the config blob and the layer blobs by their digests. A tag such as
1.0is a name that points at one manifest.
A digest is the SHA-256 hash of some content, written as sha256: followed by 64 hexadecimal characters. Because the digest comes from the content, two blobs with the same bytes always have the same digest, and changing one byte gives a different digest. This design is called content-addressable storage. Everything else in this chapter follows from it.
What `docker push` does
For each blob in the image, the Docker client asks the registry whether it already has a blob with that digest in the repository. If the blob is there, the client skips the upload. Otherwise it uploads the blob. When all blobs are in place, the client uploads the manifest last and attaches your tag to it. Pushing the manifest last means the registry never has a tag that points at a missing layer.
Watching it happen
Create a small project where each COPY step produces its own layer.
FROM alpine:3.20
WORKDIR /app
COPY config.txt .
COPY message.txt .
CMD ["cat", "/app/message.txt"]Create the two files, then build and push version 2.0. Replace YOUR_USERNAME with your Docker Hub username.
echo "setting=1" > config.txt
echo "Hello from Docker Hub" > message.txt
docker build -t YOUR_USERNAME/myapp:2.0 .
docker push YOUR_USERNAME/myapp:2.0Every layer is new to your repository, so the output looks like this. The hashes are shortened examples, and the line order varies between runs.
The push refers to repository [docker.io/YOUR_USERNAME/myapp]
a41f09c27b3e: Pushed
d93e5b10c6f8: Pushed
7be21c40a9d5: Pushed
3e8c5f1d02ab: Pushed
f1b0c7a4e682: Mounted from library/alpine
2.0: digest: sha256:9b1d... size: 1148Layers marked Mounted from already exist in another repository on the same registry. Docker Hub can link them into your repository without any upload. The manifest digest on the last line identifies this exact image.
Now change only message.txt, then rebuild and push as 2.1.
echo "Hello again" > message.txt
docker build -t YOUR_USERNAME/myapp:2.1 .
docker push YOUR_USERNAME/myapp:2.1The build cache reuses the layers for the base image, WORKDIR, and config.txt. Only the layer for message.txt is new.
The push refers to repository [docker.io/YOUR_USERNAME/myapp]
c70a2e5b19d4: Pushed
d93e5b10c6f8: Layer already exists
7be21c40a9d5: Layer already exists
3e8c5f1d02ab: Layer already exists
f1b0c7a4e682: Layer already exists
2.1: digest: sha256:41ce... size: 1148One small blob moved over the network. The rest were confirmed with a quick existence check. A real image with a 500 MB dependency layer and a 20 KB application layer behaves the same way: the big layer is uploaded once, and later pushes send only the small one.
Layer order decides how much you push
Layers form a stack. A layer's cache entry depends on the instruction and on every layer below it. If you change an early step, Docker rebuilds that layer and all layers after it, and each rebuilt layer has a new digest that must be pushed.
Compare two orderings of a typical Node.js image:
| Ordering | Effect of editing one source file |
|---|---|
| `COPY . .` then `RUN npm ci` | The copy layer changes, so the install layer is rebuilt and the whole dependency set is pushed again |
| `COPY package*.json ./`, then `RUN npm ci`, then `COPY . .` | Only the final copy layer changes, so a few kilobytes are pushed |
Put the things that change rarely (the base image, system packages, dependency installs) first. Put the things that change often (your source code) last. This speeds up local builds and also shrinks every push after the first.
The identifiers you will meet
Several different hashes appear in Docker output, and they are easy to confuse.
| Identifier | What it hashes | Where you see it |
|---|---|---|
| Image ID | The image config JSON | `docker images`, `docker image inspect` |
| Layer diff ID | The uncompressed layer tar | `.RootFS.Layers` in `docker image inspect` |
| Layer digest | The compressed layer blob as stored in the registry | Registry API, `docker push` progress lines |
| Manifest digest | The manifest JSON | The `digest:` line after a push, `RepoDigests` |
The diff ID and the registry layer digest for the same layer are usually different values, because one is computed before compression and the other after. The existence check during a push uses the registry's digest. If you compare local docker image inspect output with a registry listing and the layer hashes do not match, this is the likely reason, and it does not indicate corruption.
Inspecting digests yourself
List the diff IDs of the layers in a local image:
docker image inspect YOUR_USERNAME/myapp:2.1 --format '{{json .RootFS.Layers}}'Compare the output for 2.0 and 2.1. Every entry except the last should match.
After a push or pull, the image records the manifest digest in RepoDigests. You can use it to pull exactly that image:
DIGEST=$(docker image inspect YOUR_USERNAME/myapp:2.1 --format '{{index .RepoDigests 0}}')
echo "$DIGEST"
docker pull "$DIGEST"The value looks like YOUR_USERNAME/myapp@sha256:41ce.... RepoDigests is empty for an image that has never been pushed or pulled, so the command fails on a fresh local build.
To see the manifest as the registry stores it, including the layer digests and, for multi-platform images, the per-platform entries, run:
docker buildx imagetools inspect YOUR_USERNAME/myapp:2.1Tags move, digests do not
A tag is a mutable pointer. If you push myapp:latest today and again tomorrow, the same name points at a different manifest, and the earlier manifest stays in the repository without a tag. A digest never moves, because it is derived from the content.
This leads to practical rules:
- Use tags for human-friendly names such as
2.1orlatest. - Use a digest when you need the exact bytes, for example in a deployment manifest or when auditing what a server actually runs.
- Treat
repo:tagas "whatever the tag points to now", and record the digest if you need to know what it pointed to then.
Pushing a second tag costs almost nothing
Tagging an existing image again does not create new layers. Both tags point at the same manifest.
docker tag YOUR_USERNAME/myapp:2.1 YOUR_USERNAME/myapp:stable
docker push YOUR_USERNAME/myapp:stableEvery layer reports Layer already exists. The client sends the manifest, and the registry records a second tag for it, and the manifest digest on the last line is identical to the one for 2.1. This is how release workflows move a stable or latest tag cheaply.
Edge cases and gotchas
Rebuilds that change nothing you can see. A layer's digest depends on its exact bytes, including file timestamps and metadata. If a step downloads something that includes a build date, or a COPY picks up files whose modification times changed, the layer gets a new digest even though the content looks the same to you. A rebuild on a CI runner with a cold cache can therefore push many layers that look unchanged. Pinning base image versions, using a build cache in CI, and avoiding steps that embed timestamps all reduce this.
Base image updates. If you build on alpine:3.20 and the maintainers publish a rebuilt image under the same tag, your next build with a fresh pull uses new base layers. Your whole stack above it gets new digests, and the next push uploads the new base layers if your repository does not already have them (mounting from the official repository usually covers that case).
Existence is per repository. A blob that exists in YOUR_USERNAME/app-a is not automatically available in YOUR_USERNAME/app-b. Docker Hub often satisfies the second push with a cross-repository mount, which needs no upload, but the client still reports Mounted from rather than Layer already exists. Either way the data is not sent again.
Deleting a tag does not free space at once. Removing a tag detaches the name from a manifest. The blobs remain until the registry's cleanup removes content that nothing references. Layers shared by other images are never removed while any manifest uses them.
Interrupted pushes are safe to retry. Blobs that finished uploading stay in the repository. Running the same docker push again skips them and continues with the rest. The tag only moves once the manifest is uploaded, so a half-finished push never leaves a tag pointing at an incomplete image.
Summary of the model
- An image is a manifest, a config blob, and ordered layer blobs, each named by the SHA-256 of its content.
docker pushasks the registry about each blob, uploads only the missing ones, and uploads the manifest last.- A change in one layer invalidates the layers above it, so order your Dockerfile from least to most frequently changed.
- A tag is a movable name. A manifest digest identifies one exact image.
Exercise
You change one line in app.py. Your Dockerfile has these steps in order: FROM python:3.12-slim, COPY . /app, RUN pip install -r /app/requirements.txt, CMD ["python", "/app/app.py"]. A push after this change uploads a large layer even though the dependencies did not change. Why does this happen, and how would you reorder the Dockerfile so the next push uploads only a small layer?
Show answer
COPY . /app comes before pip install, so editing app.py changes the copy layer, and every layer after it, including the dependency install, is rebuilt with a new digest and must be pushed again. Copy only requirements.txt first (COPY requirements.txt /app/), run pip install -r /app/requirements.txt, then copy the rest of the source (COPY . /app). Now an edit to app.py changes only the final copy layer, and the registry already has the others.
Exercise
You push myapp:1.0, then run docker tag YOUR_USERNAME/myapp:1.0 YOUR_USERNAME/myapp:latest and push latest. What do you expect to see for each layer, and how do the two manifest digests compare?
Show answer
Every layer shows Layer already exists, because latest points at the same layers. The two manifest digests are identical, because both tags refer to the same manifest.