DEV Community

John  Ajera
John Ajera

Posted on

2

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! 🚀

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay