Introduction

In this tutorial, I will walk you through the process of publishing a Docker image to GitHub’s Container Registry using GitHub Actions. By using GitHub Actions, you can automate the build and publish process, making it easier to distribute your Docker images.

Prerequisites

Before getting started, make sure you have the following:

  1. A GitHub account.
  2. A Dockerfile that describes your application’s dependencies and build instructions.
  3. A GitHub repository where you want to publish your Docker image.

Step 1: Set up GitHub Actions workflow

GitHub Actions allow you to automate tasks by creating workflows.

To publish a Docker image, we’ll create a workflow file named publish.yml.

Create a new directory called .github/workflows in the root of your repository, if it doesn’t already exist.

Inside this directory, create a new file named publish.yml and add the following content:

name: Publish Docker Image

on:
  push:
    branches:
      - main

jobs:
  build-and-publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:${{ github.sha }}

This workflow will trigger whenever you push changes to the main branch. It checks out the code, logs in to GitHub’s Container Registry using your GitHub credentials, and builds and pushes the Docker image to the registry.

Step 2: Configure the Dockerfile

In your repository, create a file named Dockerfile and add the necessary instructions to build your Docker image. Here’s an example Dockerfile for a Node.js application:

FROM node:14

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm ci --only=production

COPY app.js .

CMD ["node", "app.js"]

Make sure to modify the Dockerfile to suit your application’s requirements.

Step 3: Push changes to your repository

Commit and push the Dockerfile, publish.yml, and any other files you may have modified to your repository.

Step 4: Monitor the workflow execution

After pushing the changes, navigate to the “Actions” tab in your GitHub repository. You should see the workflow you created (“Publish Docker Image”) listed there. Click on it to monitor the execution.

Step 5: Verify the Docker image in GitHub Container Registry

Once the workflow execution is complete, go to the “Packages” tab in your GitHub repository. You should see your Docker image listed there. Click on it to view the details.

Summary

You have successfully published a Docker image to GitHub’s Container Registry using GitHub Actions. By automating this process, you can easily distribute your Docker images and ensure consistent deployment across different environments. GitHub Actions provides a powerful platform for automating various tasks in your development workflow, saving you time and effort.

Related Posts