Azure CLI – Push a container to Azure Private Registry
Today in this article, we will cover how to push a container to Azure private registry using Azure CLI.
The Azure Container Registry (ACR) is a private registry that allows you to create, store, and manage container images and related artifacts. The Azure CLI lets you create an Azure container registry instance then, using Docker’s existing widely used commands we can tag and push, pull or run the container image to/from your Azure registry.
In today’s article, we will cover the below aspects,
- Create an Azure cloud resource group
- Create an Azure Container Registry
- Log in to the ACR(Azure Container Registry)
- Use Docker command to Push Image to registry
- Run Image from ACR instance
Prerequisites:
Before getting started, please make sure to install the below prerequisites on your local machine.
- Install Docker – Please see here for instruction
- Install Azure CLI – Please see here for instruction
- Azure Account with a valid subscription
Getting started
Step 1 – Login to Azure cloud subscription account
Please login to the Azure cloud subscription account using Azure CLI,
Command
az login
Step 2 – Create a Resource Groupe
Create a resource group with the az group create command.
This resource will be the logical container to manage all Azure resources.
Command
az group create --name myResourceGroup --location eastus
Example
az group create --name thecodebuzz-group --location eastus
Step 3 – Create an Azure container registry
Command
az acr create --resource-group myResourceGroup --name myRegistry --sku Basic
Example
az acr create --resource-group thecodebuzz-group --name TheCodeBuzzRegistry --sku basic
Note – Registry name must conform to the following pattern ‘^[a-zA-Z0-9]*$
For More details on the command, please visit this guideline.
Once the registry is created, you will see the below output. Please make a note of the loginServer alone which we will need in the next step,
{
"adminUserEnabled": false,
"anonymousPullEnabled": false,
"creationDate": "2022-02-01T16:48:39.075263+00:00",
"dataEndpointEnabled": false,
"dataEndpointHostNames": [],
"encryption": {
"keyVaultProperties": null,
"status": "disabled"
},
"id": "/subscriptions/000000000000000/resourceGroups/thecodebuzz-group/providers/Microsoft.ContainerRegistry/registries/TheCodeBuzzRegistry",
"identity": null,
"location": "eastus",
"loginServer": "thecodebuzzregistry.azurecr.io",
"name": "TheCodeBuzzRegistry",
"networkRuleBypassOptions": "AzureServices",
"networkRuleSet": null,
"policies": {
"exportPolicy": {
"status": "enabled"
},
"quarantinePolicy": {
"status": "disabled"
},
"retentionPolicy": {
"days": 7,
"lastUpdatedTime": "2022-02-01T16:48:40.870939+00:00",
"status": "disabled"
},
"trustPolicy": {
"status": "disabled",
"type": "Notary"
}
},
"privateEndpointConnections": [],
"provisioningState": "Succeeded",
"publicNetworkAccess": "Enabled",
"resourceGroup": "thecodebuzz-group",
"sku": {
"name": "Basic",
"tier": "Basic"
},
"status": null,
"systemData": {
"createdAt": "2022-02-01T16:48:39.075263+00:00",
"createdBy": "000000000@outlook.com",
"createdByType": "User",
"lastModifiedAt": "2022-02-01T16:48:39.075263+00:00",
"lastModifiedBy": "00000000@outlook.com",
"lastModifiedByType": "User"
},
"tags": {},
"type": "Microsoft.ContainerRegistry/registries",
"zoneRedundancy": "Disabled"
}
Step 4 – Log in to the Azure container registry
Let’s now login to the Azure container registry created in the above step,
Command
az acr login --name myRegistry
Example
az acr login --name TheCodeBuzzRegistry
Step 5 – Create Target TAG from Source Tag
This step is required to push an image to an azure registry.
Please use an existing image that you want to push to the Azure registry.
Please visit the below article, on how to create .NET Core application container and push it to DockerHub,
I already have the below docker images locally available
We will be using “thecodebuzzapp” image with TAG as “latest “
Before pushing an image to registry,you must TAG your image with the fully qualified name of your registry login server.
Command
docker tag local-image:tagname <login-server>/new-repo:tagname
Where,
- local-image – Is the name or Id of the local image. In the above example “thecodebuzzapp” is the image name.
- tagname – TAG name used for the local image. In the above example “latest” is the TAG name.
- login-server – Fully qualified login server name for your ACR instance
- new-repo – This is the target/remote repository name where you would like to push the docker local image. This new repository will be your image name as well.
- tagname – TAG name used for target/remote image
Example
docker tag thecodebuzzapp:latest thecodebuzzregistry.azurecr.io/myfirstapp:v1
Above we have created myfirstapp as a repository or placeholder and the same will also be your image name with the V1 as a tag name.
Step 6 – Push the Image to the Azure registry
Finally, let’s use the above target path of the registry to push the image to the Azure ACR instance
Command
docker push <login-server>/new-repo:tag-name
Example
docker push thecodebuzzregistry.azurecr.io/myfirstapp:v1
Verify or List container images from Azure Registry
Please use the below command to list and verify available images in the Azure registry,
Command
az acr repository list --name myRegistryName --output table
Example
az acr repository list --name thecodebuzzregistry --output table
OR
az acr repository list --name thecodebuzzregistry.azurecr.io --output table
You shall see the result below,
The login server endpoint suffix '.azurecr.io' is automatically omitted. Result ---------- myfirstapp
Run Docker Image from Azure Registry
Now, Let’s run the container image i.e myfirstapp:v1 from our container registry by using the docker run command.
Command
docker run <login-server>/new-repo:tag-name
Example
docker run thecodebuzzregistry.azurecr.io/myfirstapp:v1
And you will see you are running docker Image right from the Azure Registry.
References :
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.