DEV Community

John  Ajera
John Ajera

Posted on

How to Tag and Push a Private GitHub Container Image

How to Tag and Push a Private GitHub Container Image

GitHub Container Registry (GHCR) allows you to store and manage private container images. This guide covers logging in, pulling an image, tagging it, and pushing it to GHCR.

1. Login to GitHub CLI with Appropriate Permissions

To authenticate with GitHub and ensure you have the required permissions (write:packages), run:

gh auth login --scopes write:packages
Enter fullscreen mode Exit fullscreen mode

This command will prompt you with interactive options:

  1. Where do you use GitHub?
    • Choose GitHub.com unless you are using an enterprise instance.
  2. What is your preferred protocol for Git operations on this host?
    • Choose HTTPS for a password/token-based login.
    • Choose SSH if you use SSH keys for authentication.
  3. Authenticate Git with your GitHub credentials:
    • If using HTTPS, provide your GitHub Personal Access Token (PAT).
    • If using SSH, ensure your public key is added to GitHub.

Alternatively, you can log in to GHCR directly using this method, which avoids the interactive prompts of gh auth login and can be useful for automation or CI/CD pipelines:

echo $(gh auth token) | docker login ghcr.io -u $(gh api user --jq .login) --password-stdin
Enter fullscreen mode Exit fullscreen mode

2. Pull the Private Image

If you have an existing private image in GHCR that you want to tag, pull it first:

docker pull ghcr.io/YOUR_ORG_OR_USER/your-image:latest
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_ORG_OR_USER with your GitHub username or organization name.

3. Tag the Image

Once you have the image, tag it appropriately before pushing:

docker tag your-image:latest ghcr.io/YOUR_ORG_OR_USER/your-image:new-tag
Enter fullscreen mode Exit fullscreen mode

Replace new-tag with the desired tag (e.g., v1.0.0).

4. Push the Image to GHCR

Push the tagged image to GHCR:

docker push ghcr.io/YOUR_ORG_OR_USER/your-image:new-tag
Enter fullscreen mode Exit fullscreen mode

5. Verify the Pushed Image

To check if your image is successfully pushed, run:

docker pull ghcr.io/YOUR_ORG_OR_USER/your-image:new-tag
Enter fullscreen mode Exit fullscreen mode

or visit GitHub Packages under your repository to view the image.

Final Thoughts

Managing private container images on GHCR is straightforward with GitHub CLI and Docker commands. By following these steps, you can securely store, tag, and push your images with ease.

💡 Questions or suggestions? Drop them in the comments! 🚀

Top comments (0)