Quickstart: Deploy a container instance in Azure using the Azure CLI

Objective

The general purpose of this lab exercise is to have a first experience in deploying an existing Docker container on Azure using its service, Azure CLI. The exercise is based on a QuickStart MS Azure tutorial.

Therefore, in this exercise, we will use the Azure CLI to deploy an isolated Docker container and make its application available with a fully qualified domain name (FQDN). A few seconds after you execute a single deployment command, you can browse to the application running in the container.

Material

  • Azure Account
  • Background general principle of container-based solutions for managing execution environments
  • Use the Bash environment in Azure Cloud Shell[1]

To Do

1.     Create a resource group

Azure container instances, like all Azure resources, must be deployed into a resource group. Recall that resource groups allow you to organize and manage related Azure resources.

First, create a resource group named myResourceGroup in the eastus location with the following az group create command:

az group create --name myResourceGroup --location eastus

2.     Create a container

Now that you have a resource group, you can run a container in Azure. To create a container instance with the Azure CLI:

  • provide a resource group name, container instance name, and 
  • Docker container image to the az container create command. 

In this quickstart, you:

  • Use the public mcr.microsoft.com/azuredocs/aci-helloworld image. This image packages a simple web app written in Node.js that serves a static HTML page.
  • Deploy a container with a DNS name label so that the web app is publicly reachable[2].

Execute a command similar to the following to start a container instance. 

  • Set a --dns-name-label value that’s unique within the Azure region where you create the instance. 
  • If you receive a “DNS name label not available” error message, try a different DNS name label.
az container create --resource-group myResourceGroup --name mycontainer --image mcr.microsoft.com/azuredocs/aci-helloworld --dns-name-label lis4112 --ports 80

Within a few seconds, you should get a response from the Azure CLI indicating that the deployment has been completed. Check its status with the az container show command:

az container show --resource-group myResourceGroup --name mycontainer --query "{FQDN:ipAddress.fqdn,ProvisioningState:provisioningState}" --out table

If the container’s ProvisioningState is Succeeded, go to its FQDN in your browser. If you see a web page like the following, congratulations! You’ve successfully deployed an application running in a Docker container to Azure.

If at first the application isn’t displayed, you might need to wait a few seconds while DNS propagates, then try refreshing your browser.

3.    Pull the container logs

When you need to troubleshoot a container or the application it runs (or just see its output), start by viewing the container instance’s logs.

Pull the container instance logs with the az container logs command:

az container logs --resource-group myResourceGroup --name mycontainer

The output displays the logs for the container and should show the HTTP GET requests generated when you viewed the application in your browser.

4.    Attach output streams

In addition to viewing the logs, you can attach your local standard out and standard error streams to that of the container.

  • First, execute the az container attach command to attach your local console to the container’s output streams.
  • Once attached, refresh your browser a few times to generate some additional output. When you’re done, detach your console with Control+C. 

5.    Clean up resources

When you’re done with the container, remove it using the az container delete command:

az container delete --resource-group myResourceGroup --name mycontainer

To verify that the container has been deleted, execute the az container list command:

az container list --resource-group myResourceGroup --output table

The mycontainer container should not appear in the command’s output. If you have no other containers in the resource group, no output is displayed.

If you’re done with the myResourceGroup resource group and all the resources it contains, deletes it with the az group delete command:

az group delete --name myResourceGroup

[1] For more information, see Azure Cloud Shell Quickstart – Bash.

[2] You can expose your containers to the internet by specifying one or more ports to open, a DNS name label, or both.