In this article, Azure Cloud Shell (Azure CLI) is used to build one container image from existing application code and push it in to the Azure Container Registry (ACR). ACR Tasks contains various features within Azure Container Registry to provide efficient Docker container image builds in Azure. ACR Tasks are used to streamline various activities such as building, testing, pushing, and deploying images in Azure.

Following tasks are performed:

Create Azure Container Registry

Container registry is a service used to stores and manages container images and related artifacts. ACR, Docker Hub, and GitLab are popular options for integrating with Azure’s cloud services. Let’s created a container registry resource using the Azure CLI. Azure CLI is a free Bash shell which can be run directly within the Azure portal. It is preinstalled and configured within an account.

az group create \ 
--name <resource-group> \ 
--location <location>
az group create \ 
--name myResourceGroup1 \ 
--location eastus2 
az account list-locations
az account list-locations --query "[*].name"
az account list-locations --query "[*].name" --out tsv | sort
az acr create \ 
--name <container-registry-name> \ 
--resource-group <resource-group> \
--sku <sku-type>
az acr create \ 
--name myContainerRegistry1\ 
--resource-group myResourceGroup1 
--sku Basic

Build and Push Image through Dockerfile

In this section Azure Container Registry is used to build and push an image. Let’s first create Dockerfile and than build and push image.

echo FROM mcr.microsoft.com/hello-world > Dockerfile
echo "RUN echo 'Hello, world!' > index.html" >> Dockerfile
az acr build \ 
--image sample1/hello-world:v1\ 
--registry myContainerRegistry1\ 
--file Dockerfile .
- image:
    registry: ...
	...
Run ID: dfm was successful after 9s

Verify results

Now it’s time to verify the results. First list out the repositories and than view tags using az acr repository list and az acr repository show-tags commands.

az acr repository list \ 
--name myContainerRegistry1\ 
--output table
sample1/hello-world
az acr repository show-tags \ 
--name myContainerRegistry1\ 
--repository sample1/hello-world
--output table
v1

Run Image in ACR

Now last task is to run container image from container registry. az acr run command is used to run container image from container registry as following:

az acr run 
--registry myContainerRegistry1 \ 
--cmd '$Registry/sample1/hello-world:v1' /dev/null 

Output

Packing source code into tar to upload...
...
Run ID: cab was successful after 5s

Clean up resources

Once finished the exercise it’s recommended to delete cloud resources are being created to avoid the unnecessary resource usage and any future costs. Deleting a resource group will delete all resources contained within it. Perform following steps one by one in to Azure Portal to clean up resources:

Delete resource group - az group delete command can also be is used to remove resource group it from Azure CLI. It will remove resource group, container registry, and container images.

az group delete --name myResourceGroup1

Summary

This article described how to prepare application for containerization, create ACR instance, and store container image in Azure. Following table summarizes the key commands used in this article:

NoOperationCommandExample
1Create Resource Groupaz group createaz group create --name myResourceGroup1 --location eastus2
2Create Container Registryaz acr createaz acr create --name myContainerRegistry1 --resource-group myResourceGroup1 --sku Basic
3Build & Push image from Dockerfileaz acr buildaz acr build --image sample1/hello-world:v1 --registry myContainerRegistry1 --file Dockerfile .
4Run Image in ACRaz acr runaz acr run --registry myContainerRegistry1 --cmd '$Registry/sample1/hello-world:v1' /dev/null
5Delete Resource Groupaz group deleteaz group delete --name myResourceGroup1