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.
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
docker pulldownloads an image from a registry to this machine. - 2
alpineis 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.
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.
docker run -d -p 5001:5000 --name registry registry:2 82ea90dfa5584438b9e3a81a495aa2d23c128750d40269c7b00c0f4e96d48d75
Here is what each marked part does:
- 1
-druns the container in the background and prints its id. - 2
-p 5001:5000connects port 5001 on our machine to port 5000 inside the container. - 3
--name registrygives the container a name that we can use later. - 4
registry:2is the image that holds the registry program. The tag2is its major version.
Now pack a tiny box that only says which version it is, and push it. Run this in an empty folder:
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
printf '...' > Dockerfilewrites the text into a new file namedDockerfile. The\nmarks a new line. - 2
-qhides the build messages and prints only the image id. - 3
-t localhost:5001/hellogives the new image this name, and the.at the end is the folder to build from. - 4
docker pushuploads 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:
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
docker image rmremoves the local copy of the image. The registry keeps its own copy. - 2
docker runfinds no local image, so it pulls the image from the registry first. - 3
--rmremoves 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:
sed -i.bak 's/v1/v2/' Dockerfile
docker build -q -t localhost:5001/hello .
docker push localhost:5001/hellosha256: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
-i.bakchanges the file in place and keeps the old version asDockerfile.bak. - 2
's/v1/v2/'replaces the firstv1on each line withv2.
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?
docker image rm localhost:5001/hello
docker run --rm localhost:5001/helloUntagged: 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:
curl -s localhost:5001/v2/hello/tags/list {"name":"hello","tags":["latest"]}Here is what each marked part does:
- 1
curl -ssends a web request and prints the answer. The option-shides the progress bar. - 2
/v2/hello/tags/listis the registry's address for the list of tags in the repositoryhello.
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.
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:
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
localhost:5001/hellois the registry address and the repository, as before. - 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:
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
-Hadds a header to the request. ThisAcceptheader asks the registry for the image index. - 2
\at the end of a line means the command continues on the next line. - 3
| shasum -a 256computes 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.
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.
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.
latestis 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.