Sub topic 6
Authenticating to GHCR and setting package visibility
This chapter assumes you can build and run a Docker image, use a terminal, and have a GitHub account. You should also know roughly what docker login does. The goal is to push and pull a private image on GitHub Container Registry (GHCR) and to control who can see it.
How GHCR decides who you are
GHCR does not accept your GitHub password. Docker sends a username and a token, and GitHub checks the token's scopes against the package you are touching. For manual use on your own machine, the token is a personal access token (classic). The scopes map to actions:
| Scope | Allows |
|---|---|
| `read:packages` | Pull images |
| `write:packages` | Push and pull images |
| `delete:packages` | Delete package versions |
Fine-grained personal access tokens cannot be used with GHCR. Only classic tokens work for this registry.
Logging in and pushing
Create a classic token with write:packages, then export it into an environment variable for the current shell session. Reading it through stdin keeps it out of the process list and 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.0A successful login prints Login Succeeded. Docker stores the credential according to your credential store settings, which the previous chapter on docker login covered.
The repository path must be lowercase. A username such as MyName becomes ghcr.io/myname/my-app. Docker rejects uppercase letters in repository names.
Package visibility
The first push creates a package. By default, a package pushed from the command line is private, so only you (or your organization members with access) can pull it.
GHCR has three visibility levels:
- Private: only people and teams you grant access to can pull.
- Internal: visible to all members of an organization on GitHub Enterprise.
- Public: anyone can pull without authentication.
You change visibility in the package settings on GitHub. Open the package page, choose Package settings, and find the Danger Zone section. Making a package public is a deliberate step because it exposes every layer in the image, including anything you copied in by mistake.
Linking a package to a repository
A package can be connected to a source repository. When it is, the package inherits that repository's access permissions by default, so people with access to the repository can reach the image. You can also manage access on the package itself, granting a user, team, or organization the read, write, or admin role. Use the package-level setting when the image audience differs from the code audience.
You can connect the image automatically by adding this label to the Dockerfile. GitHub reads it at push time:
FROM alpine:3.20
LABEL org.opencontainers.image.source="https://github.com/your-github-username/my-app"
CMD ["echo", "hello"]Verifying the result
- Log out with
docker logout ghcr.io. - Try
docker pull ghcr.io/your-github-username/my-app:1.0.0. A private package should fail. - Log in again with a token that has
read:packagesand pull. It should succeed.
Common errors
denied: permission_denied: The token provided does not match expected scopesmeans the token lackswrite:packagesfor a push.unauthorizedon pull usually means you are not logged in, or the token lacksread:packages.invalid reference formatoften points to uppercase letters in the repository path.
Exercise
You pushed an image to GHCR from your laptop, and a teammate in your organization gets unauthorized when pulling it. Name two things to check.
Show answer
First, confirm the teammate is logged in to ghcr.io with a classic token that has at least read:packages. Second, check the package's access settings: the package must be shared with the teammate (directly, through a team, or by linking it to a repository they can read), or be set to public.