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:

  1. Go to your GitHub account settings.
  2. Navigate to Developer settings > Personal access tokens.
  3. 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:

  1. Open your terminal.
  2. Use the docker login command with your GitHub username and personal access token.
  3. 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:

  1. Navigate to your project directory containing the Dockerfile.
  2. Use the docker build command to build the Docker image.
  3. Tag the image with the GHCR repository URL and version.
  4. 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:

  1. Create a new YAML file under the .github/workflows directory.
  2. Define the workflow to trigger on push events.
  3. Configure the workflow to log in to GHCR using the personal access token.
  4. Specify steps to build and push the Docker image.
  5. Commit the workflow file to your repository.

Example GitHub Actions Workflow YAML:

name: Docker Image CI for GHCRon: 
push
jobs:
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:latest
build-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

--

--