Getting Started with GitHub Container Registry (GHCR)
GitHub Container Registry (GHCR) is a powerful tool that allows developers to manage Docker images alongside their source code, providing a unified development experience. In this guide, we’ll walk through the basics of setting up GHCR for your GitHub repository.
Why Use GHCR?
GHCR offers seamless integration with the GitHub ecosystem, making it easy to manage your container images, automate workflows with GitHub Actions, and control access using fine-grained permissions. It’s particularly useful if your team is already utilizing GitHub for code management, as it provides one platform for both code and containers.
Step-by-Step Setup
-
Create a Personal Access Token (PAT):
- To push images to GHCR, you’ll need a PAT with
write:packages
andrepo
permissions. - Go to GitHub Settings and generate a token with these scopes.
- To push images to GHCR, you’ll need a PAT with
-
Authenticate Docker:
- Use the PAT to log in to GHCR:
-
Build and Tag Your Docker Image:
- Build your Docker image with:
- Tag the image for GHCR:
-
Push the Image:
- Push your tagged image to GHCR:
-
Verify and Manage Your Image:
- Go to your GitHub profile and check the Packages tab to see your uploaded image.
- You can also adjust visibility settings to make it public or keep it private.
Automate with GitHub Actions
You can automate the build and push process using GitHub Actions. Set up a workflow file in your repo that triggers on code pushes to build and push your Docker image to GHCR. This ensures your images are always up to date with the latest code changes.
Example Workflow:
Automated Build and Push: The workflow includes steps to: • Check out your code. • Set up the necessary tools (QEMU and Docker Buildx). • Log in to GitHub Container Registry. • Build and push the Docker image to ghcr.io.
Your workflow is set to trigger on a push event to the main branch:
This means that whenever you push commits to the main branch, the workflow will start automatically.
Example Code
Below is an example Dockerfile you can use to build a simple Node.js application:
Dockerfile:
Conclusion
Setting up GitHub Container Registry helps simplify your development workflow by managing both code and container images in one place. With seamless integration and powerful automation capabilities, GHCR is an excellent choice for any team already working within the GitHub ecosystem.