Skip to content
Visual explainer 2h to learn 3 steps

Registries & image tagging

Push versioned images to Docker Hub, GHCR or ECR.

By Invicly Tech, , in Full DevOps with Python. About 11 minutes to read.

Topic 16 of 34

In short

A registry stores and serves images so servers and clusters can pull them. Clear tags such as a version and the git SHA tell you exactly which code is running.

Explained simply

After you build an image on your laptop, the servers that run it need a way to get it. A registry is a server that stores images so others can download them. You push an image to the registry, and servers pull it from there.

Popular registries are Docker Hub, GitHub Container Registry (ghcr.io) and Amazon ECR. They all speak the same protocol, so the same docker push and docker pull commands work with each. Only the login step is different.

An image's full name says where it lives: registry address, repository name and tag. For example ghcr.io/acme/api:1.4.0 means the api image of the acme account on GitHub's registry, version 1.4.0. If you leave out the registry, Docker assumes Docker Hub.

Tags are labels that people choose, and they can be moved to a different image later. So good teams tag every build with the git commit ID, and tag releases with a version number. Then you always know exactly which code is running.

When you push, the registry only receives the layers it does not already have. That makes pushing a new version of your app fast, because the big base layers are uploaded only once.

Press Play on a diagram to walk through it one step at a time.

SequencePushing to Amazon ECR
You
AWS CLI
Docker
ECR
  1. 1aws ecr get-login-password
    You to AWS CLI: aws ecr get-login-password
  2. 2Temporary token (12 hours)
    AWS CLI to You: Temporary token (12 hours)
  3. 3docker login with token
    You to Docker: docker login with token
  4. 4Authenticate
    Docker to ECR: Authenticate
  5. 5docker tag and docker push
    You to Docker: docker tag and docker push
  6. 6Upload missing layers
    Docker to ECR: Upload missing layers
  7. 7Image digest sha256:...
    ECR to Docker: Image digest sha256:...
CompareChoosing a registry
Docker Hub
  • Default when no host is given
  • Good for public images
  • Pull limits for anonymous users
  • Login with an access token
GHCR
  • ghcr.io/owner/name
  • Uses GitHub tokens and permissions
  • Easy from GitHub Actions
  • Private by default
Amazon ECR
  • Private, per account and region
  • IAM controls push and pull
  • 12 hour login tokens
  • Scanning and lifecycle rules

In depth

What is stored: manifests, layers and digests

A registry stores three kinds of things. Blobs are the compressed layers and the image config, each named by the sha256 of its content. A manifest is a small JSON document listing which config and layer blobs make up one image. A tag is just a name in a repository that points to one manifest.

The digest of an image is the sha256 of its manifest. Because it is a content hash, it can never point to different content. Pulling api@sha256:3f1c... always gives exactly the same image, while api:1.4.0 gives whatever the tag points to right now.

For multi-platform images, the tag points to an image index (also called a manifest list) that lists one manifest per platform, such as linux/amd64 and linux/arm64. Docker picks the right one for the machine that pulls. docker buildx build --platform linux/amd64,linux/arm64 --push builds and pushes both in one step.

MapFrom a tag to the bytes
ghcr.io/acme/api:1.4.0
  • Image index (multi-platform)
    • Points to one manifest per platform
  • Manifest linux/amd64
    • Its sha256 is the digest
    • Lists config and layers
  • Config blob
    • CMD, ENV, user, history
  • Layer blobs
    • Compressed file changes
    • Shared with other images

A tagging scheme that works

Give every image built by CI an immutable tag from the commit, such as sha-3f9a2c1 (a short git SHA). Deploy using that tag, or even better, the digest. When you cut a release, add semantic version tags to the same image: 1.4.0, plus moving tags 1.4 and 1 if others pull your image and want automatic patch updates.

latest is only a default name used when no tag is given. It does not mean newest, and nothing updates it unless you push it. Running latest in production makes it impossible to tell what is deployed and hard to roll back.

Tags must be valid: letters, digits, underscores, dots and dashes, up to 128 characters, not starting with a dot or dash. That is why a branch name like feature/login must be cleaned up before it can be a tag. Many registries can make tags immutable, so a pushed tag can never be moved to another image. ECR supports this per repository.

  • sha-3f9a2c1: every build, never moved
  • 1.4.0: a release, never moved
  • 1.4 and 1: optional moving tags for consumers
  • latest: avoid for deployments

Logging in to Docker Hub and GHCR

docker login stores credentials for one registry host in ~/.docker/config.json, or in your system's credential helper if one is configured. Always use --password-stdin so the secret does not end up in your shell history.

For Docker Hub, create a personal access token in your account settings and use it as the password. Docker Hub limits how many pulls anonymous users can make, so log in on CI runners and servers too, even when you only pull public images.

For GHCR, the user name is your GitHub user name and the password is a token. In GitHub Actions, use the built-in GITHUB_TOKEN and give the job permissions: packages: write. From your laptop, use a personal access token with the write:packages scope. New GHCR packages are private by default; you can make one public, or link it to a repository so it inherits that repository's access, in the package settings.

Amazon ECR: repositories, tokens and IAM

In ECR, each repository must exist before you push to it: aws ecr create-repository --repository-name api. The registry address is account-ID.dkr.ecr.region.amazonaws.com, one per account per region.

There is no fixed password. aws ecr get-login-password returns a token valid for 12 hours, made from your current IAM credentials, which you pipe into docker login with the user name AWS. In CI, prefer GitHub's OIDC to assume an IAM role, so no long-lived AWS keys are stored at all.

The IAM identity needs ecr:GetAuthorizationToken (on all resources) plus push actions on the repository: BatchCheckLayerAvailability, InitiateLayerUpload, UploadLayerPart, CompleteLayerUpload and PutImage. Pulling needs BatchGetImage and GetDownloadUrlForLayer.

Useful repository settings: tag immutability, scan on push for vulnerability findings, and a lifecycle policy that deletes old images, for example untagged images after 14 days, so storage costs do not grow forever.

FlowFirst push to a new ECR repository
  1. 1
    create-repository

    Once, with immutable tags and scan on push

  2. 2
    get-login-password

    12 hour token from your IAM identity

  3. 3
    docker login

    User AWS, token on stdin

  4. 4
    docker tag

    Full ECR URI plus sha tag

  5. 5
    docker push

    Only missing layers are uploaded

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Tag images with semver and git SHA~40m
  2. 2
    Push to Docker Hub or GHCR~40m
  3. 3
    Authenticate and push to ECR~40m

Key points

  • An image name is registry/repository:tag, like ghcr.io/acme/api:1.4.0.
  • Tag each build with the git SHA, and releases with a semantic version.
  • Tags can be moved; the sha256 digest always points to the same image.
  • ECR uses short-lived tokens from the AWS CLI instead of a password.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
SHA=$(git rev-parse --short HEAD)
REPO=123456789012.dkr.ecr.us-east-1.amazonaws.com/api
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
docker build -t api:$SHA .
docker tag api:$SHA $REPO:$SHA
docker tag api:$SHA $REPO:1.4.0
docker push --all-tags $REPO

Build once, tag with SHA and version, push both.

Push a release to GHCR with commit and version tags

Step 1: log in to ghcr.io with a token read from an environment variable. Step 2: build once and tag the image with the short git SHA. Step 3: add the release tags 1.4.0 and 1.4 to the same image, which costs nothing because tags are just names. Step 4: push all tags. Step 5: read back the digest and record it in your release notes or deployment file.

bash
echo "$GHCR_TOKEN" | docker login ghcr.io -u my-user --password-stdin
IMAGE=ghcr.io/acme/api
SHA=sha-$(git rev-parse --short HEAD)
docker build -t $IMAGE:$SHA .
docker tag $IMAGE:$SHA $IMAGE:1.4.0
docker tag $IMAGE:$SHA $IMAGE:1.4
docker push --all-tags $IMAGE
docker inspect --format '{{index .RepoDigests 0}}' $IMAGE:$SHA
# ghcr.io/acme/api@sha256:...

Set up an ECR repository with immutable tags and automatic clean-up

Step 1: create the repository with immutable tags and scan on push turned on. Step 2: write a lifecycle policy that expires untagged images 14 days after they were pushed. Step 3: apply the policy. Step 4: after pushing, list the images and check the scan findings. With immutable tags, pushing sha-3f9a2c1 a second time with different content fails, which protects what is deployed.

bash
aws ecr create-repository --repository-name api \
  --image-tag-mutability IMMUTABLE \
  --image-scanning-configuration scanOnPush=true
cat > policy.json <<'EOF'
{"rules":[{"rulePriority":1,"description":"expire untagged",
  "selection":{"tagStatus":"untagged","countType":"sinceImagePushed","countUnit":"days","countNumber":14},
  "action":{"type":"expire"}}]}
EOF
aws ecr put-lifecycle-policy --repository-name api --lifecycle-policy-text file://policy.json
aws ecr describe-images --repository-name api --query 'imageDetails[].imageTags'
aws ecr describe-image-scan-findings --repository-name api --image-id imageTag=sha-3f9a2c1

Quick reference

  • Full name: registry/namespace/repo:tag, e.g. ghcr.io/acme/api:1.4.0
  • No registry host means Docker Hub, so nginx is really docker.io/library/nginx:latest
  • Pull by digest for an exact image: docker pull ghcr.io/acme/api@sha256:...
  • Always log in with --password-stdin so tokens stay out of shell history
  • ECR login: aws ecr get-login-password | docker login --username AWS --password-stdin ACCOUNT.dkr.ecr.REGION.amazonaws.com
  • GHCR in Actions: log in with GITHUB_TOKEN and set permissions: packages: write
  • docker buildx imagetools inspect IMAGE shows the digest and platforms of a pushed image
  • Tags are free: tag one build with sha, version and minor version, push with --all-tags

Common mistakes

  • Deploying :latest, which makes it unclear what is running and hard to roll back.
  • Pushing to a repository that does not exist yet; ECR repos must be created first.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1What does the tag latest mean?

Question 2

2Why deploy by digest instead of by tag?

Question 3

3docker push to a new ECR repository name fails with a repository not found error. What is missing?

Question 4

4How long is the token from aws ecr get-login-password valid?

Question 5

5You push version 1.5.0, and only your code layer changed. What does the push upload?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Pull python:3.12-slim, print its full repository digest, then pull it again by that digest.

  2. Exercise 2medium

    Write a shell snippet that turns any branch name into a valid image tag, then tags and pushes an image with it and with the short SHA.

  3. Exercise 3hard

    Build a multi-platform image for linux/amd64 and linux/arm64, push it to ECR, and confirm both platforms are in the pushed index.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is GHCR free?
Public images on GHCR are free. Private images use your account's GitHub Packages storage and transfer allowance, which depends on your plan.
How do I delete old images from ECR automatically?
Add a lifecycle policy to the repository, for example to expire untagged images after 14 days or keep only the last 50 tagged images.
What does denied: requested access to the resource is denied mean on push?
You are not logged in to that registry, your token lacks write permission, or the repository name does not match your account or organisation. Check the image name and run docker login again.
Can I move an image from one registry to another without rebuilding?
Yes. Pull, retag and push it, or use docker buildx imagetools create, crane copy or skopeo copy, which copy it registry to registry without changing the digest.

Go deeper

ShareXLinkedInWhatsApp