Managing Private Registries with Azure Container Registries (with examples)
Introduction
Azure Container Registry (ACR) is a managed private registry service provided by Microsoft Azure. ACR enables you to store and manage your Docker container images securely. Using the az acr
command, you can interact with ACR to create, manage, and delete container registries. In this article, we will explore different use cases of the az acr
command with code examples.
Prerequisites
Before using the az acr
command, make sure you have the following:
- An Azure subscription
- Azure CLI (Azure Command-Line Interface) installed on your machine
- Docker installed on your machine (if you want to work with Docker images)
- Access to Azure Portal to create and manage Azure resources
Use Case 1: Create a Managed Container Registry
The az acr create
command allows you to create a new managed container registry in Azure. You need to provide a unique registry name, the resource group where the registry will be created, and the SKU (Stock Keeping Unit) which represents the pricing tier.
Code:
az acr create --name <registry_name> --resource-group <resource_group> --sku <sku>
Motivation: Creating a managed container registry is the first step towards storing and managing your Docker container images. ACR provides a secure and scalable solution for managing container images.
Explanation:
<registry_name>
: Provide a unique name for your registry.<resource_group>
: Specify the Azure resource group where the registry will reside.<sku>
: Specify the SKU (e.g., Basic, Standard, Premium) that determines the pricing tier of the registry.
Example Output:
{
"adminUserEnabled": false,
"creationDate": "2021-01-01T00:00:00+00:00",
"id": "/subscriptions/<subscription_id>/resourceGroups/<rg_name>/providers/Microsoft.ContainerRegistry/registries/<registry_name>",
"location": "westus",
"loginServer": "<registry_name>.azurecr.io",
"name": "<registry_name>",
"provisioningState": "Succeeded",
"sku": {
"name": "Premium",
"tier": "Premium"
},
"status": null,
"storageAccount": null,
"tags": {},
"type": "Microsoft.ContainerRegistry/registries",
...
}
Use Case 2: Login to a Registry
To interact with a registry, you need to authenticate yourself. The az acr login
command allows you to log in to an Azure Container Registry using Azure CLI.
Code:
az acr login --name <registry_name>
Motivation: Logging in to a registry is necessary before performing any actions such as pushing, pulling, or deleting images. Logging in ensures that you have the required permissions to access the registry.
Explanation:
<registry_name>
: Specify the name of the Azure Container Registry you want to log in to.
Example Output:
Login Succeeded
Use Case 3: Tag a Local Image for ACR
Before pushing a local Docker image to an Azure Container Registry, you need to tag it with the login server name of the registry. The docker tag
command is used for this purpose.
Code:
docker tag <image_name> <registry_name>.azurecr.io/<image_name>:<tag>
Motivation: Tagging a local image with the ACR’s login server allows you to identify the image and push it to the correct registry.
Explanation:
<image_name>
: The name of the local Docker image you want to tag.<registry_name>
: The name of the Azure Container Registry.<tag>
: The tag to assign to the image (e.g., version number).
Example Output: No output is displayed, but the image is tagged successfully.
Use Case 4: Push an Image to a Registry
To publish a Docker image to an Azure Container Registry, you need to use the docker push
command along with the fully qualified repository name.
Code:
docker push <registry_name>.azurecr.io/<image_name>:<tag>
Motivation: Pushing an image to an ACR enables you to store and share container images with your team or deploy them to Azure services like Azure Kubernetes Service (AKS).
Explanation:
<registry_name>
: The name of the Azure Container Registry.<image_name>
: The name of the Docker image.<tag>
: The tag assigned to the image.
Example Output:
The push refers to repository [<registry_name>.azurecr.io/<image_name>]
...
latest: digest: sha256:<digest_value> size: <image_size>
Use Case 5: Pull an Image from a Registry
To download a Docker image from an Azure Container Registry to your local machine, you can use the docker pull
command along with the fully qualified repository name.
Code:
docker pull <registry_name>.azurecr.io/<image_name>:<tag>
Motivation: Pulling an image from ACR allows you to retrieve container images that you can use for local development, testing, or deployment to other environments.
Explanation:
<registry_name>
: The name of the Azure Container Registry.<image_name>
: The name of the Docker image.<tag>
: The tag assigned to the image.
Example Output:
Using default tag: latest
latest: Pulling from <registry_name>/<image_name>
...
Use Case 6: Delete an Image from a Registry
To remove an image from an Azure Container Registry, you can use the az acr repository delete
command.
Code:
az acr repository delete --name <registry_name> --repository <image_name>:<tag>
Motivation: Deleting unnecessary or unused images helps to save storage space and keeps the registry organized.
Explanation:
<registry_name>
: The name of the Azure Container Registry.<image_name>
: The name of the Docker image.<tag>
: The tag assigned to the image.
Example Output: No output is displayed if the image is deleted successfully.
Use Case 7: Delete a Managed Container Registry
If you no longer need an Azure Container Registry, you can use the az acr delete
command to delete it, along with all the stored images.
Code:
az acr delete --name <registry_name> --resource-group <resource_group> --yes
Motivation: Deleting a managed container registry frees up resources and helps in cost optimization when the registry is no longer required.
Explanation:
<registry_name>
: The name of the Azure Container Registry.<resource_group>
: The Azure resource group where the registry resides.--yes
: Skip the confirmation prompt.
Example Output: No output is displayed if the registry is deleted successfully.
Use Case 8: List Images within a Registry
To see the list of images stored in an Azure Container Registry, you can use the az acr repository list
command.
Code:
az acr repository list --name <registry_name> --output table
Motivation: Listing the images within a registry helps you keep track of the available images and their versions.
Explanation:
<registry_name>
: The name of the Azure Container Registry.--output table
: Display the output in a tabular format.
Example Output:
Result
------------
<image_name>
<image_name>:<tag>
...
Conclusion
In this article, we explored different use cases of the az acr
command for managing private registries with Azure Container Registries. We covered creating a managed container registry, logging in to a registry, tagging and pushing images, pulling images, deleting images, deleting a managed container registry, and listing images within a registry. These examples provide a solid foundation for working with Azure Container Registries and integrating them into your containerized workflows.