Sub topic 5
GitHub Container Registry basics
GitHub Container Registry (GHCR) stores container images next to your code on GitHub. It is served from the host ghcr.io. This chapter covers what GHCR is, how images are named, and how to push your first image. Authentication and visibility settings are covered in the next chapter.
This chapter assumes you can build a Docker image, use the command line, and know the basics of GitHub accounts and organizations.
What GHCR is
GHCR is part of GitHub Packages, but it works differently from the older Docker registry at docker.pkg.github.com. Images in GHCR belong to a user or an organization, not to a single repository. You can link an image to a repository later, which shows it on the repository page and lets it inherit that repository's permissions.
Key properties:
- It speaks the standard OCI and Docker registry protocol, so
docker,podman, and Kubernetes work with it unchanged. - It supports multi-architecture images, because it stores manifest lists.
- Public images can be pulled anonymously. Private images need a token.
- It stores image layers as blobs, so layers shared between images are not stored twice.
How GHCR names images
A GHCR image reference has this shape:
ghcr.io/OWNER/IMAGE_NAME:TAGghcr.iois the registry host.OWNERis a GitHub user name or organization name.IMAGE_NAMEis the repository name inside the registry. It can include more path segments, such asteam/api.TAGis a label for one version of the image.
Pushing your first image
Pushing takes three steps: log in, tag the image with its full GHCR name, and push it. The login uses a personal access token (classic) with the write:packages scope, which the next chapter covers in detail. This example reads the token from the CR_PAT environment variable so it does not appear in your shell history.
echo "$CR_PAT" | docker login ghcr.io -u YOUR_GITHUB_USERNAME --password-stdin
docker tag my-app:latest ghcr.io/your-github-username/my-app:1.0.0
docker push ghcr.io/your-github-username/my-app:1.0.0docker tag does not copy any data. It adds a second name to the same local image. docker push then uploads the layers and the manifest to ghcr.io.
To check that it worked, pull the image on another machine, or remove the local copy first:
docker rmi ghcr.io/your-github-username/my-app:1.0.0
docker pull ghcr.io/your-github-username/my-app:1.0.0What happens during a push
Docker only uploads layers that GHCR does not already have. A second push of a slightly changed image is usually fast for this reason.
Where to find the image on GitHub
After the first push, the image appears under the Packages tab of your profile or organization. A new image is private by default, even if it came from a public repository. Changing visibility is covered in the next chapter.
Common mistakes
| Symptom | Likely cause |
|---|---|
| `invalid reference format` | Uppercase letters in the owner or image name |
| `denied` on push | Token lacks `write:packages`, or you do not have write access to the owner's packages |
| `unauthorized` on pull | The image is private and you are not logged in |
| Image not on the repository page | The image is not linked to the repository |
Exercise
Your GitHub organization is named AcmeCorp and you want to push my-api at version 2.1.0. Write the tag command and explain why the name looks the way it does.
Show answer
docker tag my-api:latest ghcr.io/acmecorp/my-api:2.1.0. GHCR requires lowercase references, so AcmeCorp becomes acmecorp. The docker tag command only adds a name locally, and docker push ghcr.io/acmecorp/my-api:2.1.0 uploads the image.
Exercise
You push an image from a public repository and a teammate gets unauthorized when pulling it. What is the most likely reason?
Show answer
New GHCR images are private by default, regardless of the repository's visibility. The teammate must log in with a token that can read the package, or you must change the package visibility to public.