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.
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.
Before we dive into the steps, make sure you have the following:
- A GitHub account with a repository.
- Docker installed on your local machine.
- A basic understanding of GitHub Actions.
- 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
build-and-push.yml file and add the following code:
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.
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!