Pushing Docker Images to GitHub Registry(ghcr.io): Manual and Automated Methods
Introduction
GitHub’s built-in container registry, known as GitHub Container Registry (GHCR), offers a convenient way to publish Docker images directly from your GitHub repository. In this guide, we’ll walk through the process of publishing Docker images to GHCR and automating the publishing process using GitHub Actions.
What is GitHub Container Registry?
GitHub Container Registry allows developers to store and share Docker container images directly within their GitHub repositories. This feature enables seamless integration of containerized applications with GitHub’s ecosystem.
Step 1: Creating a Personal Access Token
To interact with GHCR, we need to create a personal access token with appropriate permissions. Follow these steps:
- Go to your GitHub account settings.
- Navigate to Developer settings > Personal access tokens.
- Create a new token with sufficient permissions for Docker login, image read/write, and deletion.
Step 2: Logging into GHCR with Docker
Before pushing Docker images to GHCR, you need to log in using Docker with your personal access token. Here’s how:
- Open your terminal.
- Use the
docker login
command with your GitHub username and personal access token. - Specify the GHCR endpoint (ghcr.io) for the registry URL.
Example Docker Login Command:
docker login --username mert2m --password your_personal_access_token ghcr.io
Step 3: Building and Pushing Docker Images
Now, let’s build your Docker image and push it to GHCR:
- Navigate to your project directory containing the Dockerfile.
- Use the docker build command to build the Docker image.
- Tag the image with the GHCR repository URL and version.
- Push the image to GHCR using the docker push command.
Example Docker Build and Push Commands:
docker build . -t ghcr.io/mert2m/actions-runner-controller-ghcr:latest
docker push ghcr.io/mert2m/actions-runner-controller-ghcr:latest
Step 4: Automating Image Publishing with GitHub Actions
To automate the process of publishing Docker images whenever code changes occur, we’ll set up a GitHub Actions workflow:
- Create a new YAML file under the .github/workflows directory.
- Define the workflow to trigger on push events.
- Configure the workflow to log in to GHCR using the personal access token.
- Specify steps to build and push the Docker image.
- Commit the workflow file to your repository.
Example GitHub Actions Workflow YAML:
name: Docker Image CI for GHCRon:
pushjobs:
build_and_publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build and push the image
run : |
docker login --username mert2m --password ${{ secrets.GH_PAT }} ghcr.io
docker build . --tag ghcr.io/mert2m/actions-runner-controller:latest
docker push ghcr.io/mert2m/actions-runner-controller:latestbuild-and-deploy:
runs-on: self-hosted
steps:
- name: deploy on your cluster
run: |
kubectl --kubeconfig /Users/mertpolat/.kube/config apply - f /Users/mertpolat/actions-runner/_work/actions-runner-controller/actions-runner-controller/deployment.yaml