AWS ECR Alternative - How to Build and Push Docker Images to GitHub Container Registry with GitHub Actions
Back to Blog page

AWS ECR Alternative - How to Build and Push Docker Images to GitHub Container Registry with GitHub Actions

In this blog post, we explore an alternative to Amazon Elastic Container Registry (ECR) by leveraging the capabilities of GitHub Container Registry (GHCR). We provide a step-by-step guide on how to build and push Docker images to GHCR using GitHub Actions. By utilizing GHCR, developers can seamlessly integrate Docker image management into their GitHub workflows, simplifying the CI/CD process.

Published by PJ

PJ Jul 11, 2023 3 min read

Introduction:

The Amazon Elastic Container Registry (ECR) has been a popular option for many developers and DevOps teams when it comes to creating and deploying Docker images. However, GitHub Container Registry (GHCR) is a great choice if you're seeking an alternative to ECR. GHCR enables you to manage and store your Docker images right inside GitHub repositories. In this blog post, we'll look at utilising GitHub Actions to create and upload Docker images to GHCR.

Why GitHub Container Registry?

Your current GitHub operations are seamlessly integrated with GitHub Container Registry. It makes use of GitHub Actions' strength to let you automate the creation, testing, and distribution of your Docker images. You may manage and interact with your team more easily by using GHCR to maintain your source code and Docker images in one location.

Prerequisites:

Before we dive into the steps, make sure you have the following:

  1. A GitHub account with a repository.
  2. Docker installed on your local machine.
  3. A basic understanding of GitHub Actions.
  4. A Project with Dockerfile in the root level

Step 1: Create a Workflow File

To get started, navigate to the root directory of your GitHub repository and create a new directory called .github/workflows. Inside this directory, create a new file named build-and-push.yml. This file will contain the configuration for our GitHub Actions workflow.

Step 2: Configure the Workflow

Open the build-and-push.yml file and add the following code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 name: Build and Push Docker Image on: push: branches: - main env: IMAGE_NAME: your-image-name TAG: ${{ github.sha }} jobs: build-and-push: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout Repository uses: actions/checkout@v3 - name: Login to GitHub Container Registry uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and Push Docker Image run: | docker build -t ghcr.io/${{ github.repository }}:${{ env.TAG }} . docker push ghcr.io/${{ github.repository }}:${{ env.TAG }}

Every push to the main branch initiates the execution of this workflow. It creates the Docker image, pushes it to GHCR with the supplied image name and tag, and logs in using the GitHub token.

Make sure to replace your-image-name with the desired name for your Docker image.

Step 3: Commit and Push

Save the changes to the build-and-push.yml file and commit it to your repository. Push the commit to trigger the workflow.

Step 4: Monitor the Workflow

To monitor the workflow, navigate to the "Actions" tab in your GitHub repository. You should see a workflow named "Build and Push Docker Image" running. Click on it to view the workflow's progress.

Step 5: Verify the GHCR Docker Image

When the procedure is finished successfully, select the "Packages" tab in your repository. Your Docker image will be accessible under the GitHub Container Registry area. To view the available tags and further information, click on it.

Conclusion:

In this blog post, we looked at utilising GitHub Actions to create and upload Docker images to the GitHub Container Registry. You may streamline your development and deployment procedures and simplify your Docker image administration by using GHCR. You may improve your CI/CD workflows and boost the effectiveness of your software development lifecycle with GitHub Actions' automation features.

So give it a shot and experience the potential of GitHub Container Registry for your hosting needs for Docker images. Happy building!

Note: GitHub Container Registry is an evolving service, and it's recommended to refer to GitHub's official documentation for any recent updates or changes to the service.

Code - Link to Github repo

If you enjoyed this article, please click the 👍 button and share to help others find it!

Tagged with:
Docker
GitHub
1
0
Share
Enjoyed this article?
Leave a comment below!
Previous Article
Next Article