DEV Community

Renicius Pagotto
Renicius Pagotto

Posted on • Originally published at renicius-pagotto.Medium

Azure Container Registry — Storing images and artifacts

Hi everyone, in today’s post we’re going to talk about Azure Container Registry, a great alternative to Docker Hub for storing both public and private images and artifacts.

We’ll also learn how to use Azure CLI and Docker CLI for creating and uploading Docker images.

What is Azure Container Registry?

It's a service for storing and managing images and artifacts, based on Docker Registry technology.

Azure Container Registry supports OCI distribution, which is a vendor-agnostic specification responsible for enabling the storage, sharing, and protection of artifacts and images.

What are Artifacts?

Artifacts are objects used by a process, such as Dockerfile, appconfig, appsettings, Helm charts, and others.

Basic Concepts of Azure Container Registry

Azure Container Registry has some basic concepts like namespaces, repositories and tags that are essential for proper usage of the resource.

Repository

A repository is a collection of images or artifacts with the same name but different tags. A repository may include a namespace to help identify related repositories. Namespaces are used by separating names with a slash.

Here we have some examples of repositories

  • productapi:v1
  • productapi:v2
  • productapi:latest

Here are some examples of repositories with namespace as prefix

  • product/productapi:v1
  • product/catalogapi:v1
  • notification/emailapi:v1
  • notification/pushapi:v1

Tag

A tag is essentially a way to version an image or artifact. We can have different versions of the same artifact, each with a different tag.

Here we have some examples with tags where the tags are represented by :v1, :v2, :latest

  • customerapi:latest
  • customerapi:v1
  • customerapi:v2

Demonstration

In today’s demonstration, we will explain the step-by-step process of provisioning an Azure Container Registry account through the Azure Portal and also via Azure CLI. Additionally, we will demonstrate how to create and upload an image to the registry.

Prerequisites

  • Azure account
  • Visual Studio Community
  • Azure CLI
  • Docker Desktop

Provisioning Azure Container Registry via the Azure Portal

Open the Azure Portal and in the search bar, type the name of the resource, in this case, Azure Container Registry, and then select it.

Image description

On the next screen showing the resource list, click on + Create.

Image description

On this screen, we need to provide some information that will be used to create the resource.

Image description

For better understanding, here is a detailed explanation of each piece of information:

Subscription: The name of the subscription that will be used for provisioning the new service.

Resource Group: The name of the resource group to be used, which is associated with the subscription. If you don't have one, select the option Create new below the dropdown to create a new one.

Registry name: The name to be given to the service.

Location: The region where the service will be provisioned.

Availability Zones: When selected, this offers high availability and resilience for all registries.

SKU: The tier of service desired. Available tiers are Basic, Standard, and Premium, each with its own features.

Next, click on Review + Create. After the resource manager validates the information, click on Create.

Once the provisioning of the new Azure Container Registry account is complete, access it and select Repositories from the sidebar menu.

Image description

Within the Repositories option is where images are stored. However, since we just created the resource, the repository is empty.

Image description

Provisioning Azure Container Registry via Azure CLI

Another way to provision Azure Container Registry is through Azure CLI, the command line interface. To do this, we will use a series of commands detailed below.

With Azure CLI installed on your machine, the first step is to authenticate to your Azure account using the following command:

az login

Once authenticated, you need to set the subscription that will be used for provisioning resources. Use the following command:

az account set -s name-of-subscription

The az account command sets which resource we want to access in Azure, in this case, our subscription. The set command specifies that we want to define a specific subscription, and the -s argument is where we pass the name of the desired subscription.

After authentication and setting the subscription, we will provision a new Azure Container Registry resource using the following command:

az acr create -g "resource-group-name" -n "resource-name" --sku sku-name

The az acr create command specifies the desired resource and the action to be taken. The acronym acr stands for Azure Container Registry, and the action create is used to create the resource. Other available actions include delete, update, list, and more.

The -g or --resource-group argument is where you define the resource group to be used, which must be associated with a subscription. The -n or --name argument is used to name the resource, and finally, the --sku argument specifies the desired service tier. If no information is provided, the resource will be created with the Standard tier by default.

After executing this command, a new Azure Container Registry resource will be provisioned, and you can view it through the Azure Portal.

An important point to note is that each Azure Container Registry resource has a URL for managing artifacts. This information can be found on the resource’s page and is labeled as the Login server.

Image description

Creating the Project and Dockerfile

For today’s demonstrations, we need a simple API project that includes a Dockerfile for creating a Docker image.

To do this, we will create a new project in Visual Studio with the Docker option enabled.

Image description

With the creation of the new project completed, note that the solution includes a Dockerfile with all the necessary configurations, ready to use. Keep in mind that this Dockerfile is provided with the project template.

Image description

Image description

We have now completed the creation of a basic project, using the default template provided by .NET. However, the most important part is the Dockerfile, which we will use to build our image.

Creating and Uploading the Image with Docker CLI

With Docker CLI properly installed on your machine, the first step is to create an image of our application using the Dockerfile.

Open your command line terminal, navigate to the root folder of the application we just created, and execute the following command to build the image:

docker build -t image-name -f AzureApiContainer/Dockerfile .

The docker build command specifies the desired action. The -t argument is where we pass the desired name for our image, the -f argument specifies the location of the application’s Dockerfile, and the final dot represents the context for executing the Dockerfile.

Once the image is built, run the docker images command to verify the image. In my case, I named the image azureapicontainer.

Image description

Now it’s time to rename the image to include the URL of our registry server (Login server), which is the Azure Container Registry.

docker tag azureapicontainer:latest pagottoacr.azurecr.io/azureapicontainer:v1

A new image will be created using the name and tag we just specified.

Image description

The command for adding a new tag along with the service URL works as follows:

docker tag image-name:tag login-server/destination-image-name:tag

The docker tag command creates a new version of the same image with a different tag and name. We need to specify both the source image and the destination image. For the destination image, we must prefix the name with the registry server URL, which is the Login Server URL from the Azure Container Registry page mentioned earlier.

With the image ready for upload, we first need to authenticate with the Azure Container Registry. Use the following command for authentication:

az acr login -n azure-container-registry-name

The az acr login command performs the authentication (login) to the Azure Container Registry (acr). The -n argument should be followed by the name of the Azure Container Registry account/service used to store the image.

After authentication, we are ready to upload the image using the following command:

docker push server-image-name:tag

The docker push command uploads the image, and you need to specify the image to be used. In this case, we use the image with the registry server name. Docker will use the specified server name to connect and perform the upload.

In my case, I used the following command to upload the image:

docker push pagottoacr.azurecr.io/azureapicontainer:v1

Image description

Creating and Uploading the Image with Azure CLI

To create an image using Azure CLI, we will use Azure Container Registry Tasks (ACR Tasks), which are commands for creating, sending (uploading), and executing a container image natively in Azure, without the need to have Docker installed locally.

To create an image via Azure CLI, open your command line terminal, navigate to the root folder of the application, and execute the following command:

az acr build -t "image-name" -r registry-name -f dockerfile-directory .

The az acr build command specifies the desired action, in this case, creating a new image. The -t argument is where you define the name of your image. The -r argument specifies the name of the Azure Container Registry you will use. The -f argument is the location of the Dockerfile for your application, and the final dot represents the context in which the Dockerfile will be executed.

When you run the command, the creation and upload of the image will be handled automatically by Azure Container Registry.

Image description

Viewing the Image in Azure Container Registry

After creating and uploading an image via Docker CLI or Azure CLI, open the Azure Portal again and select the Azure Container Registry resource that you provisioned.

Image description

Image description

In the sidebar menu, select Repositories.

Image description

With that, you can finally see our image stored in the service.

Image description

By accessing the repository, we can view the different available versions.

Image description

With this, our image is stored in Azure Container Registry and available for use in any process.

Did you enjoy this post? Want to chat more about Azure Container Registry? Leave a comment with your questions and ideas or connect with me on LinkedIn.

Referências
Azure Container Registry Documentation

Top comments (0)