Post

How to Deploy an Azure Container Instance using the Azure CLI

The Azure command line interface (CLI) can be used to create a container group and deploy container images as Azure Container Instances.

In this tutorial, we will review the Azure CLI container commands you will need to be familiar with to deploy a container image as an Azure Container Instance.

This content is available in video form on the Cloud Engineer Skills YouTube channel.

Deploying an Azure Container Instance using the Azure CLI

Creating the Resource Group

Make sure to login first and select the correct Azure subscription.

1
az login

We will create the resource group using the az group create command.

1
az group create --name cloudengineerskills-aci-rg --location eastus

Creating the Container Instance inside the Container Group

Now with the resource group, we will now use the az container create command to create the container group inside the resource group and get it to run a particular image which will create that container instance inside the container group.

1
az container create --resource-group cloudengineerskills-aci-rg --name containergroup --image mcr.microsoft.com/azuredocs/aci-helloworld --dns-name-label cloudengineerskills-helloworld-aci --ports 80 --cpu 2 --memory 2

In the command above we have used the following parameters on the az container create command.

  • --resource-group: the name of the resource group the container group will be created under
  • --name: the name of the container group to be created
  • --image: the container image that will be used for the container instance within the containe group
  • --dns-name-label: that will be prepended to .eastus.azurecontainer.io that we can use to locate the running container in the browser
  • --ports: the ports that will be opened on the container instance, seperated by a space
  • --cpu: the number of CPU cores allocated to the container group to be shared across container instances within the group
  • --memory: the amount of memory (RAM) in GB (accurate to one decimal place) allocated to the container group to be shared across container instances within the group

For a full list of parameters available on the create command take a look at the docs.

View the list of container groups inside a resource using the az container list command.

1
az container list --resource-group cloudengineerskills-aci-rg --output table

Next we will access the fully qualified domain name and provisioning state of the container instances within a container group using the az container show command.

1
az container show --resource-group cloudengineerskills-aci-rg --name containergroup --query "{FQDN:ipAddress.fqdn,ProvisioningState:provisioningState}" --out table

Once the provisioning state reaches “Succeeded”, if you visit the fully qualified domain name in a browser you should see your container image running using the ACI helloworld container.

Monitoring a Container Instance

We can use the az container logs and az container attach CLI commands to monitor the logs produced by the container instances running within a container group.

To get a print the logs to screen of a container in the container group, use the az container logs command and specify the resource group, and container group name.

1
az container logs --resource-group cloudengineerskills-aci-rg --name containergroup

If you what to stream the logs produced by the container instance in the group name and print them to screen, use the az container attach command and specify the resource group, and container group name.

1
az container attach --resource-group cloudengineerskills-aci-rg --name containergroup

Cleaning up the Resources

We will now delete the container group we created above using az container delete CLI command and verify the container group has been deleted by lists all of the container groups within the resource group using az container list.

If we run the following az container list command passing the resource group as a parameter we will see a list of container groups inside the resource group in a tabular format.

1
az container list --resource-group cloudengineerskills-aci-rg --output table

We should see one entry for the container group named containergroup which is using the aci-helloworld image.

Next we will delete this container group which will also delete the container instance using the az container delete command passing the resource group and the name of the container group as parameters.

1
az container delete --resource-group cloudengineerskills-aci-rg --name containergroup

You can verify the container group has been deleted by running the az container list command again.

1
az container list --resource-group cloudengineerskills-aci-rg --output table

Finally we will delete the resource group.

1
az group delete --name cloudengineerskills-aci-rg

Other Azure CLI Container Instance Commands

  • az container exec: execute a command from within a running container of a container group
  • az container export: export a container group in yaml format
  • az container restart: restarts all containers in a container group
  • az container start: starts all containers in a container group
  • az container stop: stops all containers in a container group

Further Reading

This post is licensed under CC BY 4.0 by the author.