Using GitHub Container Registry in Practice (2024)

Using GitHub Container Registry in Practice (3)

Did you know that GitHub has launched the new GitHub Container Registry September 2020?

That’s great news for people who use GitHub packages already, as GitHub has launched many new capabilities for container users. In this article, let’s discuss the things you should know about the latest GitHub Container Registry. 😊

Here is what’s covered in this article.

  • Introduction to Container Registry
  • The new capabilities of GitHub Container Registry
  • How to migrate to GitHub Container Registry?
  • A Use Case — Deploying an Nginx webserver to Kubernetes with GitHub Actions, Kubernetes, and GitHub Container Registry.

Let’s start with a quick introduction to Container Registries. If you are already familiar with that, feel free to skip the following section.

A Container Registry is a place to store and manage container images. These container images can be either Public and Private. The Public container images are accessible by anyone, whereas you can configure access control for Private container images.

Examples of popular container registries are DockerHub, Azure Container Registry, Amazon Elastic Container Registry(ECR).

So, why do we need a container registry in the first place?

Ease of Cloud Deployment

Today most of the applications are running on the cloud. Major cloud providers support downloading container images from popular container registries to deploy applications on demand.

Ease of Container Management

The container registry also helps to centrally manage application container images efficiently across teams within your organization.

Additional Features!

Some container registries have additional features such as image scanning for vulnerabilities, geo replications for high availability, etc.

Now that we know about Container Registries, Let’s get to know about the newly announced GitHub Container Registry.

Using GitHub Container Registry in Practice (4)

GitHub Container Registry allows organizations to seamlessly host and manage their container images with fine-grain permissions. It can store both Private and Public container images as well.

Let’s look at the following table that compares and contrasts GitHub Container Registry vs. other popular container registries.

One thing to note is that the GitHub Container Registry is currently in public beta and the pricing model for private images, General Availability and SLA are subject to change.

Prior to GitHub Container Registry, GitHub used GitHub Packages Docker Registry for storing Docker images.

However, the GitHub Container registry supersedes the GitHub Packages Docker registry and optimizes the support for containers’ unique needs by introducing many new features.

Have a look at the following new features introduced with GitHub Container Registry. You can read more about them here.

1. Ability to store container images under your organization

GitHub Container Registry allows you to create container images under your organization. Now you can easily share those container images with your teams securely.

2. Fine-grained permissions for the container images

You don’t have to issue the repository permission to container users. Instead, you can now assign the least privilege permission for the individual users. That will ensure better security for your container images.

3. Can access public container images anonymously

Some of the other container registries require authentication to access public images. But GitHub Container Registry allows you to access any public image without authentication

Let’s imagine you have docker images already in the GitHub Packages Docker registry and want to migrate them to GitHub Container Registry. How can you do that?

Well, there are a few things you should keep in mind before the migration.

GitHub Container Registry uses a new domain i.e.ghcr.io for docker images instead of the old domaindocker.pkg.github.com which is used by the GitHub Packages Docker Registry.

Have a look at the example docker image URLs below.

Example URL — GitHub Packages Docker Registry

docker.pkg.github.com/OWNER/REPOSITORY/IMAGE_NAME

Example URL — GitHub Container Registry

ghcr.io/OWNER/IMAGE_NAME

At the moment of this writing, GitHub Container Registry supports only PAT (Personal Access Token) to authenticate and access the container images owned by your organization.

Creating a PAT

When creating a PAT, we need to give the following permissions. Follow this documentation for more information.

  • read:packages Scope to download container images and read their metadata.
  • write:packages Scope to download and upload container images and read and write their metadata.
  • delete:packages Scope to delete container images.

Save the PAT as an environment variable

export CR_PAT=<TOKEN>

Authenticating with GitHub Container Registry

echo $CR_PAT | docker login ghcr.io -u kasunsjc --password-stdin
Using GitHub Container Registry in Practice (5)

Once we are authenticated, we are ready to push images to GitHub Container Registry. However, we need to tag the image before it’s pushed to the registry.

You should follow the format ghcr.io/USERNAME/IMAGENAME:TAG when tagging a container image.

docker image tag nginx:latest ghcr.io/kasunsjc/nginx:v1
Using GitHub Container Registry in Practice (6)

After the image is built and tagged, then you are ready to push it to the registry. Use the following command to push the image to GitHub Container Registry.

docker image push ghcr.io/kasunsjc/nginx:v1
Using GitHub Container Registry in Practice (7)

Once it’s pushed to the container registry, you can verify your image by navigating to the Packages section in the GitHub as shown below.

Using GitHub Container Registry in Practice (8)

Now that the images are on GitHub Container Registry, how do we pull them into our local machine or to your build server?

Well, You can easily use the docker image pullcommand. Have a look at the following example of pulling an Nginx image.

Using GitHub Container Registry in Practice (9)

Now imagine, you want to make the image that is pushed to the GitHub Container Registry public, so everyone can access it.

How can you do that?

GitHub Container Registry supports both public and private images. We can make an image public as shown below.

Note: Making an image public is NOT bidirectional, so if you decide to make an image repository public, there no way of making the image back to private.

Using GitHub Container Registry in Practice (10)

Now that we understand about GitHub Container Registry, let’s use it with other services such as GitHub Actions and Kubernetes.

In this use case, we are going to deploy an Nginx webserver to Kubernetes. During that process, we will be using GitHub Container Registry to store the built image and GitHub actions to build and push the image to the Registry.

Have a look at the following diagram to get the overall picture of what we are going to accomplish.

Using GitHub Container Registry in Practice (11)

Let’s use the following instructions in the dockerfile to dockerize a simple web page that is served by Nginx.

FROM nginx:latest
COPY ./index.html /usr/share/nginx/html/index.html

Then we use the following Workflow file to inform GitHub Actions to perform the listed jobs to push the image to the Container Registry and Deploy to Azure Kubernetes Service.

If you are new to GitHub Action, refer to the article below and understand it better. You can also visit this GitHub link to access the demo files.

Using Github Actions with Azure App Services for Web AppsIn the modern development environment, automated application deployment plays a vital role in the organization's…blog.bitsrc.io

After successful execution of the GitHub Action, the application is deployed to Kubernetes and you should see the following output.

Using GitHub Container Registry in Practice (12)

You can now access the webpage by browsing to the Kubernetes Load Balancer IP.

Using GitHub Container Registry in Practice (13)

You can also verify if the application is using the correct image from the GitHub Container Registry using the kubectl decribe pod command as shown below.

Using GitHub Container Registry in Practice (14)
Using GitHub Container Registry in Practice (2024)

FAQs

Can you use GitHub as a container registry? ›

GitHub Packages offers different package registries for commonly used package managers, such as npm, RubyGems, Apache Maven, Gradle, Docker, and NuGet. GitHub's Container registry is optimized for containers and supports Docker and OCI images.

Why use container Registry? ›

It helps the organization control access, and may also provide the ability to scan the registry contents for known vulnerabilities. Additionally, enterprises seeking a private container registry should look for the following features: Binary-level image scanning from a regularly updated vulnerability database.

What is the difference between container registry and repository? ›

While a container repository is a collection of related container images that manage, pull and push images, a container registry is a collection of repositories that store container images.

Do not use a UID below 10,000.? ›

UIDs below 10,000 are a security risk on several systems, because if someone does manage to escalate privileges outside the Docker container their Docker container UID may overlap with a more privileged system user's UID granting them additional permissions.

Is GitLab a container registry? ›

In the GitLab container registry, you can use the OCI 1.1 manifest subject field to associate container images with Cosign signatures. You can then view signature information alongside its associated container image without having to search for that signature's tag.

Is Google Container Registry a Docker registry? ›

Container Registry is a private Docker repository that works with popular continuous delivery systems. It runs on GCP's Andromeda based network fabric to provide consistent uptime on an infrastructure protected by Google's security.

How many container registries should I have? ›

Just use 1 registry. One big registry makes it easier for security. You only have to scan one registry and you can integrate security much earlier in your deployment process (scan the staging image in advance because this is the container that you will deploy in prod).

What is an example of a container registry? ›

Examples include Docker Hub, Amazon ECR, and Azure.

What is the difference between container Registry and artifacts registry? ›

Artifact Registry supports fine-grained access control via Cloud IAM. You can store your artifacts regionally and multi-regionally, Unlike Container Registry where you don't have an option to store your artifacts regionally. It caches the most frequently requested Docker Hub images on mirror.gcr.io.

Is Kubernetes a container registry? ›

The registry is a stateless, scalable server side application that stores and lets you distribute container images. The Kubernetes registry is an image pull secret that your deployment uses to authenticate with an image registry.

What is the difference between container registry and Artifactory? ›

Container Registry used to store container images only. In Artifact registry we can store container images as well as we can store multiple artifact formats, including OS packages , as well as language packages for popular languages like Python, Java, and Node.

Is Docker Hub a registry or repository? ›

Docker Hub is a container registry built for developers and open source contributors to find, use, and share their container images. With Hub, developers can host public repos that can be used for free, or private repos for teams and enterprises.

What is the best practice for Dockerfile layers? ›

Placing infrequently changing instructions at the top and frequently changing ones at the bottom of Dockerfile is a best practice for achieving this. To optimize your Dockerfile and image building process: Utilize multi-stage builds: Multi-stage builds help reduce the number of layers in the final image.

Why do people use UID? ›

A UID (Unique Identifier) is a numeric or alphanumeric string that uniquely identifies a single entity within a given system. The advantages of using a UID are improved data integrity throughout the asset lifecycle, increased productivity and cost savings (through better logistics and engineering analysis).

What is the difference between UID and GID? ›

Unix-like operating systems identify a user by a value called a user identifier, often abbreviated to user ID or UID. The UID, along with the group identifier (GID) and other access control criteria, is used to determine which system resources a user can access.

Is the GitHub Docker Registry free? ›

GitHub Packages usage is free for public packages. For private packages, each account on GitHub.com receives a certain amount of free storage and data transfer, depending on the account's plan.

How to make GitHub container registry public? ›

Next to the organization, click Settings. On the left, click Packages. Under "Package Creation", choose whether you want to enable the creation of public, private, or internal packages. To enable organization members to create public packages, click Public.

Can I run GitHub in Docker? ›

Docker provides a set of official GitHub Actions for you to use in your workflows. These official actions are reusable, easy-to-use components for building, annotating, and pushing images. The following GitHub Actions are available: Build and push Docker images : build and push Docker images with BuildKit.

Are GitHub Actions containers? ›

GitHub Actions allows you to run a job within a container, using the container: statement in your workflow file. For more information, see "Running jobs in a container." To process container-based jobs, the self-hosted runner creates a container for each job.

References

Top Articles
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 6642

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.