Sub topic 9
Choosing a registry and logging in
This chapter assumes you can write a Dockerfile, read a GitHub Actions workflow, and know what a tag is. It covers where a CI pipeline sends the image it builds and how the pipeline proves it may do so.
What a registry does
A container registry is a server that stores images and serves them by name. An image name has four parts: the registry host, a repository path, a tag, and optionally a digest. For example, ghcr.io/example-org/example-app:1.4.0 names the host ghcr.io, the repository example-org/example-app, and the tag 1.4.0. If you leave out the host, Docker assumes Docker Hub.
Choosing a registry
The choice mostly follows where your code and your team already live.
| Registry | Host | Good fit when |
|---|---|---|
| GitHub Container Registry | `ghcr.io` | Your code is on GitHub and you want CI to log in without extra secrets |
| Docker Hub | `docker.io` | You publish public images that people expect to find there |
| Amazon ECR | `<account>.dkr.ecr.<region>.amazonaws.com` | You deploy on AWS and want IAM-based access control |
| Google Artifact Registry | `<region>-docker.pkg.dev` | You deploy on Google Cloud |
Check three things before you commit. Look at who can pull, since public and private repositories behave differently. Look at rate limits, because anonymous pulls from Docker Hub are limited. Look at how the CI job authenticates. A registry in the same platform as your CI usually offers a simpler, safer path.
This tutorial uses ghcr.io. The same steps apply elsewhere, with a different host and different credentials.
How login works in the workflow
A registry only accepts pushes from an authenticated identity. In CI you log in before the push step. The docker/login-action action runs docker login for you and stores the result for later steps in the same job.
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}Three details matter here:
registryselects the host. Without it, the action logs in to Docker Hub.usernameis the user who triggered the run, fromgithub.actor.passwordissecrets.GITHUB_TOKEN, a token that GitHub creates for each run and revokes when the run ends. You do not create it or store it in the repository.
The if condition skips login on pull requests. Those runs only build the image and do not push it, so they need no registry access. Pull requests from forks also get a read-only token, so a login there would not allow pushing anyway.
Giving the token permission to push
GITHUB_TOKEN can only do what the workflow's permissions block grants. Pushing to ghcr.io needs the packages scope:
permissions:
contents: read
packages: writeListing permissions explicitly removes every scope you did not name. Here the job can read the repository and write packages, and nothing else.
Common errors
denied: permission_denied: write_packageusually means thepermissionsblock lackspackages: write, or the package is linked to a different repository.unauthorizedon a pull-request run means login was skipped butpushwas still true. Tie both to the same condition.- An image name with uppercase letters is rejected. Repository names must be lowercase, and
github.repositorycan contain capitals in an organization name.
Exercise
Your workflow logs in to ghcr.io with GITHUBTOKEN, but the push fails with "writepackage" denied. What do you check first, and why does the workflow skip login on pull requests?
Show answer
Check that the workflow's permissions block includes packages: write, because the token only holds the scopes the block lists. Login is skipped on pull requests because those runs build without pushing, so they need no registry write access, and fork pull requests receive a read-only token anyway.