Why latest is not a version

A sealed box waits in a depot until a server asks for it by name. A tag can point at a different image tomorrow, and a digest always names exactly one image.

Part 3 of 9 · Containers5 min read
registry: the depotMondayTuesdaylatesta tag is a paper label: it can movelatest =no tag givendigest: a fingerprintsha256:9e01…exactly one imagechecked on every pull

Your server ran myapp:latest yesterday. It runs myapp:latest today, and nobody redeployed anything. Was it the same image both days?

In part 2, Bub packed a sealed box. Now the box has to travel from the machine that built it to every server that runs it. On the way, the box waits somewhere, and something has to say which box is which.

The depot

Every port has a depot, a yard where packed boxes wait between ships. Bub leaves a box there, and later a ship’s crew collects it by name.

For images, that yard is a registry. Uploading a box is a push, and fetching it is a pull.

An image name tells Bub where to go. localhost:5001/hello:latest has three parts: the registry’s address (localhost:5001), the repository (hello), and, after the colon, a tag (latest). If you omit a part, Docker adds it. Plain alpine means docker.io/library/alpine:latest.

Shell
docker pull alpine  
Using default tag: latest
latest: Pulling from library/alpine
Digest: sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6
Status: Image is up to date for alpine:latest
docker.io/library/alpine:latest

Here is what each marked part does:

  1. 1docker pull downloads an image from a registry to this machine.
  2. 2alpine is the image name. We give no registry and no tag, so Docker fills in both.

We typed one word, and Docker went to Docker Hub’s library shelf for the box labelled latest. We come back to the Digest line below.

localhost:5001/hello:latestregistrythe depotrepositorythe shelftagthe labellocalhost:5001/hello@sha256:45f0…digestthe fingerprint
An image name says where the depot is, which shelf, and which box. A label names a box, and a digest is its fingerprint.

A depot of our own

A registry is itself a small program in an image. So we can run one on our own machine, with no account. We use port 5001 because on macOS the AirPlay receiver already listens on port 5000.

Shell
docker run -d -p 5001:5000 --name registry registry:2    
82ea90dfa5584438b9e3a81a495aa2d23c128750d40269c7b00c0f4e96d48d75

Here is what each marked part does:

  1. 1-d runs the container in the background and prints its id.
  2. 2-p 5001:5000 connects port 5001 on our machine to port 5000 inside the container.
  3. 3--name registry gives the container a name that we can use later.
  4. 4registry:2 is the image that holds the registry program. The tag 2 is its major version.

Now pack a tiny box that only says which version it is, and push it. Run this in an empty folder:

Shell
printf 'FROM alpine\nCMD ["echo", "hello from v1"]\n' > Dockerfile 
docker build -q -t localhost:5001/hello .  
docker push localhost:5001/hello 
sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef
Using default tag: latest
The push refers to repository [localhost:5001/hello]
4056dcb4ac68: Pushed
a9986cd6f37d: Pushed
latest: digest: sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef size: 855

Here is what each marked part does:

  1. 1printf '...' > Dockerfile writes the text into a new file named Dockerfile. The \n marks a new line.
  2. 2-q hides the build messages and prints only the image id.
  3. 3-t localhost:5001/hello gives the new image this name, and the . at the end is the folder to build from.
  4. 4docker push uploads the image to the registry that its name points at.

We never wrote latest. Docker added that label because we gave none. Two lines say Pushed. One is alpine’s one sheet, and the other is a small build record that BuildKit attaches to the box. Our CMD added no sheet, only a note, as in part 2. Your hashes will differ, because they depend on the base image and the moment of the build. The output here comes from the default containerd image store in Docker Desktop. A classic Linux Docker Engine pushes no build record, so it prints one line fewer.

Delete the local copy and ask the depot for the box by name, as a fresh server would:

Shell
docker image rm localhost:5001/hello 
docker run --rm localhost:5001/hello  
Untagged: localhost:5001/hello:latest
Deleted: sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef
Unable to find image 'localhost:5001/hello:latest' locally
latest: Pulling from hello
4056dcb4ac68: Download complete
Digest: sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef
Status: Downloaded newer image for localhost:5001/hello:latest
hello from v1

Here is what each marked part does:

  1. 1docker image rm removes the local copy of the image. The registry keeps its own copy.
  2. 2docker run finds no local image, so it pulls the image from the registry first.
  3. 3--rm removes the container when it stops.

The box came back from the depot and said what we packed.

The paper label

A tag is a paper label on a box, and nothing fixes it in place. Anyone allowed to push, Bub included, can remove the label and put it on a different box.

Change one word in the Dockerfile and push again, under the same name:

Shell
sed -i.bak 's/v1/v2/' Dockerfile  
docker build -q -t localhost:5001/hello .
docker push localhost:5001/hello
sha256:df594e019b2fc5774076816bab512ee3c4f6fb6769617df8619c2a146ac19668
Using default tag: latest
The push refers to repository [localhost:5001/hello]
43f8d28f1a2e: Pushed
a9986cd6f37d: Layer already exists
latest: digest: sha256:df594e019b2fc5774076816bab512ee3c4f6fb6769617df8619c2a146ac19668 size: 855

Here is what each marked part does:

  1. 1-i.bak changes the file in place and keeps the old version as Dockerfile.bak.
  2. 2's/v1/v2/' replaces the first v1 on each line with v2.

Alpine’s sheet was already in the depot. So of the lines the push prints, only the build record is new. The new CMD note travels in a new config, which the push does not list. Now act as the fresh server again, with the exact command from before:

Same name, same command as last time. What prints now?

Shell
docker image rm localhost:5001/hello
docker run --rm localhost:5001/hello
Untagged: localhost:5001/hello:latest
Deleted: sha256:df594e019b2fc5774076816bab512ee3c4f6fb6769617df8619c2a146ac19668
Unable to find image 'localhost:5001/hello:latest' locally
latest: Pulling from hello
43f8d28f1a2e: Download complete
Digest: sha256:df594e019b2fc5774076816bab512ee3c4f6fb6769617df8619c2a146ac19668
Status: Downloaded newer image for localhost:5001/hello:latest
hello from v2

The same name now gives a different box. A tag points at exactly one image at a time, and a push to that tag moves the tag. Ask the registry which labels it holds:

Shell
curl -s localhost:5001/v2/hello/tags/list  
{"name":"hello","tags":["latest"]}

Here is what each marked part does:

  1. 1curl -s sends a web request and prints the answer. The option -s hides the progress bar.
  2. 2/v2/hello/tags/list is the registry's address for the list of tags in the repository hello.

The registry holds one label. The v1 box is still on the shelf, but no label points at it any more.

So the answer to the opening question depends on the server. A server that pulled yesterday keeps the box it already has, so it opens that box and runs v1. docker run pulls only when the name is missing locally. A server that restarts on a fresh machine, or runs docker pull first, gets v2. Two servers that run “the same” myapp:latest can run different code.

hello v1sha256:45f0…latesthello v1sha256:45f0…latest v2sha256:df59…hello v1sha256:45f0… v2sha256:df59…latest

The v1 box sits on the hello shelf with the paper label latest.

The fingerprint

Every push printed a second name: sha256: and 64 hex characters. That second name is the digest, the box’s fingerprint. Nobody chooses the digest. It is computed from what is inside the box.

Ask the depot for the box by its fingerprint instead of its label:

Shell
docker run --rm localhost:5001/hello@sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef  
Unable to find image 'localhost:5001/hello@sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef' locally
localhost:5001/hello@sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef: Pulling from hello
4056dcb4ac68: Download complete
Digest: sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef
Status: Downloaded newer image for localhost:5001/hello@sha256:45f029dd31148ac2d43578af0a66d325778592f17805d48dc8b9afc669f998ef
hello from v1

Here is what each marked part does:

  1. 1localhost:5001/hello is the registry address and the repository, as before.
  2. 2@sha256:... asks for the image by its digest instead of by a tag.

The label moved to v2, and the fingerprint still found v1.

The fingerprint comes from a short JSON document that the registry stores behind each tag. Here the document is an image index, which points at a manifest for each platform. The digest is the SHA-256 of the index’s exact bytes. Fetch the index and hash it yourself:

Shell
curl -s -H 'Accept: application/vnd.oci.image.index.v1+json' \  
localhost:5001/v2/hello/manifests/latest | shasum -a 256 
df594e019b2fc5774076816bab512ee3c4f6fb6769617df8619c2a146ac19668  -

Here is what each marked part does:

  1. 1-H adds a header to the request. This Accept header asks the registry for the image index.
  2. 2\ at the end of a line means the command continues on the next line.
  3. 3| shasum -a 256 computes the SHA-256 hash of the bytes it receives. The final - in the output stands for that input.

The hash matches the digest of v2. The index names each manifest by its digest. Each manifest names the config and every sheet by their own digests. Change one byte in any sheet, and every fingerprint above it changes, up to the one we hashed. On a classic Docker Engine store, the tag points straight at a manifest. There, the header to ask for is application/vnd.docker.distribution.manifest.v2+json. A digest cannot be moved to another box, because another box has another digest. When you pull by digest, Docker checks the bytes it receives against the digest.

tagdigest latest sha256:45f0…chosen by a personcan be movedcomputed from the contentscannot be movedchecked on pull
A tag is a label that someone puts on a box and can remove. A digest is computed from the contents and always points at the same box.

latest only means no tag was given

latest is only the name Docker uses when you do not give a tag. It does not mean newest. Nothing updates latest when you push 1.4.2. The latest label can be on a box built a year ago, and it can move while your deploy is half done.

So when the question “what is running?” needs one answer, pin a digest. Write myapp@sha256:… in the deploy file, or myapp:1.4.2@sha256:…. In the second form, Docker ignores the tag and uses the digest, so the tag is there only for people to read. A tag that your team never moves, such as the commit hash, is the next best choice. But only the digest is enforced by the depot. When you try an image on your laptop, a tag is fine.

need to knowexactly what runs?yespin a digestmyapp@sha256:…noa tag is finemyapp:latest
Pin a digest where you need to know exactly what runs. A tag is fine for trying an image locally.

Key points

  • A registry is the depot. Images are pushed to it and pulled from it by name, registry/repository:tag.
  • A tag is a movable label. The same tag can name a different image tomorrow, and two servers can hold different images under it.
  • latest is only the default tag name. It does not mean newest.
  • A digest is the SHA-256 of the document behind the name. That document names everything below it by digest, down to every layer. A digest names exactly one image, and Docker checks it on pull.
  • In production, pin a digest so “what is running?” has one answer.

Next, in part 4: who opens the box, and what dockerd, containerd and runc each do.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS