Sub topic 8
Why pushes are fast the second time: layers and digests
This chapter assumes you can build an image from a Dockerfile, log in to Docker Hub, and run docker push on a tagged image. Here you will see why a second push of a similar image uploads almost nothing.
An image is a stack of layers
A Docker image is not one big file. It is a list of layers plus a small JSON file called the manifest. Each layer is a compressed archive of filesystem changes, such as files added or modified by one Dockerfile instruction. The manifest lists the layers in order and points to an image configuration blob.
Every layer, the manifest, and the configuration are stored as blobs. Each blob is named by its digest. A digest is a SHA-256 hash of the blob's content, written like sha256:3b1f.... The same content always produces the same digest, and different content produces a different one.
Because the name comes from the content, a registry can tell whether it already has a blob without comparing file contents. It only needs to check the digest.
What docker push does
When you push, the Docker CLI does the following for each layer:
- It asks the registry whether a blob with that digest already exists in the target repository.
- If it exists, the CLI skips the upload and reports
Layer already exists. - If it does not exist, the CLI uploads the compressed layer.
- After all layers are present, the CLI uploads the manifest. The tag then points at that manifest.
Seeing it happen
Use the Dockerfile from earlier in this tutorial and push it once. Then add one line that changes only a small file:
FROM alpine:3.20
RUN echo "version 2" > /version.txt
CMD ["echo", "Hello from Docker Hub"]Build and push it as a new tag:
docker build -t YOUR_USERNAME/myapp:1.1 .
docker push YOUR_USERNAME/myapp:1.1The output looks roughly like this:
The push refers to repository [docker.io/YOUR_USERNAME/myapp]
a1b2c3d4e5f6: Pushed
08000c18d16d: Layer already exists
1.1: digest: sha256:9f8e... size: 735The Alpine base layer already exists in Docker Hub, so only the new RUN layer and the manifest are sent. Layers are shared across tags in the same repository, so a small change costs a small upload.
Tags and digests are different things
A tag is a mutable label. A digest identifies exact content. You can move the tag latest to a new image, but a digest always refers to the same bytes.
The last line of push output prints the digest of the manifest. You can pull by digest to get exactly that image:
docker pull YOUR_USERNAME/myapp@sha256:PASTE_DIGEST_HERETo list the digests your local images have, run:
docker images --digests YOUR_USERNAME/myappWhy layer order matters
A layer is reusable only if its content and all the layers beneath it are unchanged. Docker's build cache works the same way. If you change an early instruction, every later layer is rebuilt and gets a new digest, so it must be uploaded again.
Put rarely changing steps first, such as installing dependencies. Put frequently changing steps last, such as copying your source code.
Common mistakes
- Expecting a rebuild to give the same digest. Layer digests can change when file timestamps or build inputs change, even if the files look the same. Pin base image versions and avoid commands that embed the current time.
- Treating a tag as a guarantee. If someone pushes a different image to the same tag, the tag now points elsewhere. Use the digest when you need a fixed reference.
- Putting
COPY . .before dependency installation. Any source change invalidates the dependency layer, so it is rebuilt and re-pushed each time.
Exercise
You push myapp:1.0, then change one line of application code in the last Dockerfile instruction and push myapp:1.1. Why does the second push upload far less data, and what always gets uploaded even if no layer changed?
Show answer
Layers are identified by content digests, and the registry already holds every layer below the changed instruction, so the CLI prints "Layer already exists" for them and uploads only the changed layer. The manifest is always uploaded, because the new tag needs a manifest that lists the layers and has its own digest.