Deploying containers on Microsoft Azure

Karan Singh
6 min readJul 4, 2020

Containers are modern way of deploying easy applications, with minimal hardware requirements. Due to containers, one can run multiple applications on single piece of hardware. Containerized applications are easily replacing virtualized environments due to the absence of guest operating systems for virtual machines. That’s why, containers have become an essential building block in the field of DevOps. In this article, we are going to explore steps to deploy containerized applications (on cloud) on Microsoft Azure, and without actually installing any dependencies on your local machine, using two of its services: Azure Container Registry and Azure Container Instance.

Introduction to the services

Azure Container Registry

As the name suggests, Azure Container Registry is used to build and store the containerized applications/images. We can create a storage registry on Microsoft Azure platform, where we can place all our containerized applications. It is connected with many other of Azure’s services like Azure Kubernetes Service, App Service and Machine Learning. It also provides Geo-replication to manage a single registry across multiple regions. It also provides automatic container building and patching for the containers. The service is based on the Docker Registry 2.0, which is open sourced, hence it acts like a private Docker Registry service. To build container images in Azure, we can use an existing environment, or we can use a development pipeline, or we also use Azure Container Registry Tasks to do so.

Azure Container Instances

Azure Container Instance acts as an on-demand server less Azure environment to deploy our container images. It helps in running multiple container images, isolated from each other. Azure Container Instances provide several advantages over using a virtual machine, as we don’t need to provision and manage the underlying infrastructure to run our containerized applications, with quicker start-up times compared to virtual machines. It also provides us with ability to customize our container instance, according to our own needs. Container Instances provides ability to run both Linux and Windows containers, since both require different set of specifications. Container instance can also interact with other resources on Microsoft Azure platform, by deploying it in our virtual network.

Practical: Deploying container images of Azure

We are now going to see the steps required to set-up two Azure services, Azure Container Registry and Azure Container Instance to run container images. So now let’s see, what steps we need to follow to run container images on Microsoft Azure, and that too without actually installing any external dependencies on our local machine. Here are the steps:

  1. First, we require a Microsoft Azure id to deploy our resources. Hence, we need to sign-up for Microsoft Azure using the link: Create free Azure account (Do note, we require a credit card for registering at Microsoft Azure)

2. Now, we need to login into Azure portal using our registered credentials in previous steps.

3. Now, open the Azure cli by clicking the icon on the right side of search bar of Azure portal. For using the Azure cli, we will need to provision cloud storage. This step will be done automatically by Azure, once you’re setting up the Azure cli.

4. Once the Azure cli is opened, we are ready to write our first command! In this command we are going to create a resource group. In this resource group, we going to add all our resources. This will make monitoring and management easier. Type the following command in Azure cli:

$ az group create — name learn-deploy-acr-rg — location <add-a-location>

<add-a-location> should be replaced by one of specific locations of Microsoft Azure data centres around the world. In our case, we used ‘westus’. To learn more about locations offered by Microsoft Azure, go to the link: Microsoft Azure: Locations

5. Now, we have to create a variable named ‘ACR_Name’, to store the name of our Azure Container Registry. The container registry name must be unique within Azure and contain between 5 and 50 alphanumeric characters. Write the following in Azure cli:

$ ACR_NAME=<registry-name>

6. Now, we are going to create a Azure Container Registry, where we are going to store our container images. To create it, type the following command in Azure cli:

$ az acr create — resource-group learn-deploy-acr-rg — name $ACR_NAME — sku Premium

You can expect an output in JSON format, similar to below:

{
"adminUserEnabled": false,
"creationDate": "2020-07-03T08:44:20.426739+00:00",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/myACR0007",
"location": "westus",
"loginServer": "myacr.azurecr.io",
"name": "myACR",
"provisioningState": "Succeeded",
"resourceGroup": "learn-deploy-acr-rg",
"sku": {
"name": "Premium",
"tier": "Premium"
},
"status": null,
"storageAccount": null,
"tags": {},
"type": "Microsoft.ContainerRegistry/registries"
}

7. Now, we need to create a Dockerfile in our Container Registry. So, simply enter the command :

$ code

A editor will get opened in Azure cli, and enter the following code. Use Ctrl+s (for Windows) or Cmd+s (for Mac) to save the file.

FROM node:9-alpine
ADD https://raw.githubusercontent.com/Azure-Samples/acr-build-helloworld-node/master/package.json /
ADD https://raw.githubusercontent.com/Azure-Samples/acr-build-helloworld-node/master/server.js /
RUN npm install
EXPOSE 80
CMD [“node”, “server.js”]

Above is a simple hello world nodejs application from Azure Samples Github repository. You can change the Dockerfile, according to your liking.

8. Now, run the following command into the Azure Cli command window to build the container image.

$ az acr build --registry $ACR_NAME --image helloacrtasks:v1 .

9. To verify if the image is running correctly or not, run the following command:

$ az acr repository list --name $ACR_NAME --output table

You can expect to see the following output:

Result
-------------
helloacrtasks

10. Now, we are going to deploy containers on Azure Container Instance. For that, first we need to enable registry admin account to deploy containers on instance. For that, we need to run following command in Azure Cli:

$ az acr update -n $ACR_NAME --admin-enabled true

Now, run the following command to get credentials of admin account. Please note the username and password generated. Here’s the command:

$ az acr credential show --name $ACR_NAME

11. Now, execute the following command, to run the container image stored in container instance.

$ az container create \
--resource-group learn-deploy-acr-rg \
--name acr-tasks \
--image $ACR_NAME.azurecr.io/helloacrtasks:v1 \
--registry-login-server $ACR_NAME.azurecr.io \
--ip-address Public \
--location <location> \
--registry-username [username] \
--registry-password [password]

Replace [username] and [password] with the credentials generated by executing command in Step 10. Replace the <location> from the value of location, which was added while creating the resource group. In our case, <location> has value of ‘westus’.

12. Now, we need to execute one final command to get the IP address of hosted container. Here’s our final command:

$ az container show --resource-group  learn-deploy-acr-rg --name acr-tasks --query ipAddress.ip --output table

Note the the value of the IP address, that was generated by running the above command. Enter the IP address into your browser, and you can expect to see the following output:

So, that’s it! You’ve just loaded your hello world container to Azure Container Registry and ran it on Azure Container Instance. Here’s a good learning source by Microsoft Learn that helped me in learning this exercise. It will help you to execute this exercise step by step: Microsoft Learn: Build and store container images with Azure Container Registry

--

--

Karan Singh

Microsoft Student Partner | Samsung Brand Ambassador | Bachelors in Computer Science Student | Aviation geek | Formula One Fan