# Registries: Docker Hub, GHCR, ECR and tags you can trust

Share images through a registry: name and push them, log in to Docker Hub, GHCR and ECR, and tag so every deploy is traceable. Tags can move; a digest never does, and this lesson proves it.

Course: [Docker](https://devops.rajeev.pro/courses/docker), lesson 8. About 30 minutes, intermediate. Source: https://devops.rajeev.pro/courses/docker/08-registries

## What you will learn

- Read a full image name, registry, repository, tag and digest
- Push and pull images to a registry, and see that shared layers upload once
- Log in to Docker Hub, GitHub Container Registry and Amazon ECR without leaking a password
- Explain why a tag can change while a digest can't
- Tag images by commit SHA and version, and pin deploys by digest

## What's in a name

An image lives in your local Docker until you push it to a **registry**, a server that stores images so
other machines can pull them. The full name says where:

Before registries, teams copied image files between machines or rebuilt them separately in each environment.
Those copies drifted, and a release was hard to identify after the fact. A registry gives the image one shared
home; a complete image reference identifies that registry, repository and version. A tag is convenient for
people, while a digest identifies exact image content. In this lesson, you'll push to a local registry, compare
tags with digests, and choose a safe reference for a deployment.

```text
ghcr.io / acme/price-api : 1.4.0  @sha256:034074b2…
registry   repository     tag      digest
```

Leave out the registry and Docker assumes Docker Hub: `nginx:1.30-alpine` is really
`docker.io/library/nginx:1.30-alpine`. To push, you tag the image with the registry's name, then push. Try it
with a registry running on your own machine:

```sh
$ docker run -d --name registry -p 127.0.0.1:5000:5000 registry:3
$ docker tag price-api:multi localhost:5000/price-api:1.0.0
$ docker push localhost:5000/price-api:1.0.0
96ed2737ae31: Pushed
a5789fc40e82: Pushed
b8b68e3af840: Pushed
1.0.0: digest: sha256:034074b20fb92e756118038e8c32abeb0c269da12b78451263de0b83cdef6ccd size: 856
$ docker tag price-api:multi localhost:5000/price-api:sha-3f9c2e1
$ docker push localhost:5000/price-api:sha-3f9c2e1
3214acf345c0: Layer already exists
7c12895b777b: Layer already exists
sha-3f9c2e1: digest: sha256:034074b20fb92e756118038e8c32abeb0c269da12b78451263de0b83cdef6ccd size: 856
$ curl -s localhost:5000/v2/price-api/tags/list
{"name":"price-api","tags":["1.0.0","sha-3f9c2e1"]}
```

The second push uploaded nothing: layers are stored by their hash, and the registry already had them. Two
tags, one digest.

## Tags move, digests don't

A **tag** is a name that points at an image. A **digest** is the SHA-256 hash of the image's manifest: change
one byte of content and the digest changes. Watch what happens when a different build is pushed as `1.0.0`:

> **Figure.** Animated. A registry holds tags, which are names, and manifests, identified by digest. Pushing price-api:1.0.0 stores the 14.6MB multi-stage image, digest sha256:034074b20fb9, and points the tag 1.0.0 at it. Pushing the same image as sha-3f9c2e1 uploads nothing new; only a second name is added. Then the 508MB single-stage build, digest sha256:367778fc7a08, is pushed as 1.0.0 again and the tag silently moves to it. Pulling :1.0.0 now gets the 508MB root image, while pulling by digest always gets the original.
>
> Same tag, different content, no warning. Only the digest is a promise.

```sh
$ docker tag price-api:single localhost:5000/price-api:1.0.0
$ docker push localhost:5000/price-api:1.0.0
1.0.0: digest: sha256:367778fc7a085fc1131a5d04d26bb8ffb0e48d45bde2b5aa67bbd4bae48c7881 size: 856
$ docker pull localhost:5000/price-api@sha256:034074b20fb92e756118038e8c32abeb0c269da12b78451263de0b83cdef6ccd
Status: Downloaded newer image for localhost:5000/price-api@sha256:034074b2…
```

The registry accepted the overwrite without a word. A server that pulled `1.0.0` yesterday and one that pulls
it today now run different code. The digest still fetches the original. Find an image's digest with
`docker buildx imagetools inspect localhost:5000/price-api:sha-3f9c2e1` or in the `push` output.

## Docker Hub

**Docker Hub** is the default registry and home of the official images. Create a free account, then:

```sh
$ docker login -u yourname                    # prompts for a personal access token
$ docker tag price-api:multi yourname/price-api:1.0.0
$ docker push yourname/price-api:1.0.0
```

Use a **personal access token** from your account settings, not your password, and give it only the scope it
needs (read-only for servers that just pull). Docker Hub limits how many images can be pulled per hour, with
lower limits for anonymous pulls, so log in on CI runners and servers too; the current limits are in the
_usage and limits_ page linked below. Free accounts can keep public repositories, plus a limited number of
private ones.

## GHCR and ECR

Most teams keep images next to their code or their cloud. **GitHub Container Registry** (`ghcr.io`) uses your
GitHub login and permissions:

```sh
$ echo "$GHCR_TOKEN" | docker login ghcr.io -u yourname --password-stdin
$ docker tag price-api:multi ghcr.io/yourname/price-api:1.0.0
$ docker push ghcr.io/yourname/price-api:1.0.0
```

In GitHub Actions, the job's own `GITHUB_TOKEN` can push, given `permissions: packages: write` (the CI/CD
course builds this workflow). New packages are private until you change their visibility.

**Amazon ECR** gives each AWS account a registry, logged in to with a 12-hour token from the AWS CLI:

```sh
$ aws ecr create-repository --repository-name price-api --image-tag-mutability IMMUTABLE
$ aws ecr get-login-password --region eu-west-1 \
    | docker login --username AWS --password-stdin 123456789012.dkr.ecr.eu-west-1.amazonaws.com
$ docker tag price-api:multi 123456789012.dkr.ecr.eu-west-1.amazonaws.com/price-api:sha-3f9c2e1
$ docker push 123456789012.dkr.ecr.eu-west-1.amazonaws.com/price-api:sha-3f9c2e1
```

`IMMUTABLE` makes ECR reject a push to an existing tag, which turns the overwrite you saw above into an error.
In CI, get AWS credentials through OIDC instead of stored keys (CI/CD course, lesson on secrets).

> **Never docker login -p.** `docker login -p secret` leaves the password in your shell history and in the process list. Pipe it in with `--password-stdin`. `docker login` saves credentials in `~/.docker/config.json`, only base64-encoded unless a credential helper (the OS keychain, `pass`) is set up: never commit or copy that file.

> **Video.** [Push Your Docker Image to the World (Hub, GHCR, ECR)](https://www.youtube.com/watch?v=I7lWwX4rvwQ&t=120s) (Dargslan)

## Tagging best practices

| Tag                            | Use                                                                 |
| ------------------------------ | ------------------------------------------------------------------- |
| `sha-3f9c2e1` (the git commit) | **every** build: says exactly which code is inside, never reused    |
| `1.4.0`, plus `1.4` and `1`    | releases; the short ones move on purpose to the newest patch        |
| `main`, `pr-42`                | the latest build of a branch or pull request, for test environments |
| `latest`                       | convenience only: never deploy it, never pin it                     |

Deploy by **digest**, or by the SHA tag, never by a tag that moves. Then you can always say what's running, and
a rollback means pointing back at an earlier digest that is still in the registry. Set a cleanup policy too:
registries keep every untagged layer until you delete it, and storage costs money.

Treat tags as labels for finding an image, not as proof that it cannot change. Release aliases such as `1.4` may
intentionally move when a patch is published, but a running production service should point to immutable content.
Record the digest during CI and deploy that value. Keep enough old digests to support the rollback window, then
expire images according to a retention rule. A policy that deletes everything old can make a rollback impossible;
a policy that deletes nothing quietly grows storage use.

> **In production.** Push from CI only, with a token that can write to one repository. Make release tags immutable where the registry supports it, keep images for as long as you might roll back to them, and record the digest you deployed alongside the release.

> **Video.** [Container Images and Layers: Copy-on-Write, Digests and Image Bloat | Containers Ep 6](https://www.youtube.com/watch?v=jZ9iFXuQ-1c&t=395s) (Joshua Moses | Cybertrace Foundation)

## Mission: Prove a tag can lie

Run a private registry on your machine, push one image under a version tag and a commit tag, then push a different image under the same version tag. Show the digests change, and pull the original back by digest.

- You need: Docker and the two price-api images from lesson 7
- You'll have: Two different digests for one tag, and a successful pull by the original digest

```sh
$ docker run -d --name registry -p 127.0.0.1:5000:5000 registry:3
$ docker tag price-api:multi localhost:5000/price-api:1.0.0
$ docker push localhost:5000/price-api:1.0.0 | tail -1              # note the digest
$ docker tag price-api:multi localhost:5000/price-api:sha-3f9c2e1
$ docker push localhost:5000/price-api:sha-3f9c2e1 | grep -c "already exists"
$ docker tag price-api:single localhost:5000/price-api:1.0.0
$ docker push localhost:5000/price-api:1.0.0 | tail -1              # a different digest
$ docker pull localhost:5000/price-api@sha256:<the first digest>
$ docker rm -f registry
```

Same tag, two digests: the first push's content is still there, reachable only by its digest or by the
`sha-3f9c2e1` tag. That's why deploys pin one of those.

## Try it

- Run a local registry with `docker run -d -p 127.0.0.1:5000:5000 registry:3` and push an image to it.
- Push the same image under a second tag and look for `Layer already exists`.
- Run `curl -s localhost:5000/v2/price-api/tags/list` to see the tags the registry holds.
- Push a different image under an existing tag and compare the digest it prints.
- Pull an image by its digest with `docker pull name@sha256:…`.
- Find your Docker Hub pull limit in the Docker Hub documentation.

## Quiz

1. In ghcr.io/acme/price-api:1.4.0, what is ghcr.io?
   - The tag
   - **The registry host; acme/price-api is the repository and 1.4.0 the tag** (answer)
   - The image ID
   - A Docker Hub user
   Why: With no host, Docker assumes Docker Hub (docker.io), and nginx alone means docker.io/library/nginx:latest.

2. You push price-api:1.0.0, then push a different build as price-api:1.0.0. What happens?
   - The push is rejected
   - **The tag silently moves to the new image; anyone pulling 1.0.0 now gets different content** (answer)
   - Both images share the tag
   - The old image is renamed
   Why: Tags are mutable pointers unless the registry enforces immutability (ECR and others can). Digests are hashes of the content and can't move.

3. Why did pushing the same image under a second tag upload nothing?
   - The push failed
   - **Layers are stored by their hash; the registry already had them, so only a new tag was added** (answer)
   - Docker compresses tags
   - Second tags are free on Docker Hub
   Why: The push printed "Layer already exists" for every layer. The same applies across images sharing a base.

4. What is the safest way to log in to a registry from a script or CI?
   - docker login -p mypassword
   - **Pipe a short-lived token in with --password-stdin, from a secret store** (answer)
   - Commit ~/.docker/config.json
   - Put the password in the Dockerfile
   Why: -p puts the password in shell history and the process list. In CI use the job's token (GITHUB_TOKEN for GHCR, OIDC role for ECR).

5. Which tag tells you exactly which code an image was built from?
   - latest
   - **A tag with the git commit SHA, such as sha-3f9c2e1** (answer)
   - stable
   - prod
   Why: Tag every build with its commit SHA, add a version tag on releases, and deploy by digest or by that never-reused SHA tag.

## Resources

- [Docker Docs: docker image push](https://docs.docker.com/reference/cli/docker/image/push/) (Docker Docs)
- [Docker Docs: Docker Hub usage and limits](https://docs.docker.com/docker-hub/usage/) (Docker Docs)
- [GitHub Docs: working with the Container registry](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry) (GitHub Docs)
- [AWS Docs: pushing a Docker image to Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html) (AWS Docs)
- [Distribution registry](https://distribution.github.io/distribution/) (CNCF Distribution)
