Deploying a container instance using the Azure CLI

  1. Home
  2. Deploying a container instance using the Azure CLI

Return to AZ-104 Tutorial

You’ll learn how to launch serverless Docker containers using Azure CLI for Azure Container Instances. When you don’t require a whole container on an orchestration platform like Azure Kubernetes Service, you’ll be able to deploy an application to a container instance on-demand.

Prerequisites:

  • In Azure Cloud Shell, use the Bash environment.
  • Install the Azure CLI if you prefer to execute CLI reference commands locally. Consider running Azure CLI in a Docker container if you’re using Windows or macOS.
    • Sign in to the Azure CLI using the az login command if you’re using a local installation. Follow the instructions in your terminal to complete the authentication procedure.
    • When requested, install Azure CLI extensions the first time you use it.
    • To determine the version and dependent libraries that are installed, run az version. Run az upgrade to get the most recent version.
  • The Azure CLI version 2.0.55 or later is required for this quickstart. The most recent version of Azure Cloud Shell is already installed.

Also, here you’ll use the Azure CLI to deploy the container and make its application available with a fully qualified domain name. So, let’s begin:

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, that can be used both Bash and PowerShell with Cloud Shell to work with Azure services.

To run the code in Azure Cloud Shell, so follow the following steps:

  1. First of all, begin Cloud Shell.
  2. Secondly, select the Copy button on a code block to copy the code.
  3. After this, paste the code into the Cloud Shell session by selecting Ctrl+Shift+V.
  4. Subsequently, select Enter to run the code.

Create a resource group

Azure container cases should be used in a resource group. Resource groups enable to organize and manage related Azure resources.

So, first, create a resource group named myResourceGroup in the eastus place with the following command:

az group create --name myResourceGroup --location eastus

Creating a container

Now run a container in Azure. And, to create a container with the Azure CLI, you must provide a resource group name, container instance name, and Docker container image to the az container generate command.

Also, you can expose the containers to the internet by designating one or more ports to open, a DNS name label, or both.

In order to do so, set a –dns-name-label the value that’s unique within the Azure region where you create the instance. If in case 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 aci-demo --ports 80
Practice Test for AZ-104

Pulling the container logs

If you require to troubleshoot a container or the application it runs, begin by viewing the container instance’s logs.

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

Attaching output streams

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

So, first, execute the az container attach command to connect the local console to the container’s output streams:

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

Cleaning up resources

After finishing with the container, delete it using the az container delete command:

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

And, to verify that the container has been deleted, execute the following list command:

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

The mycontainer container must not rise in the command’s output.

And, after completing with the myResourceGroup resource group and all the resources it contains, delete it with the az group delete command:

az group delete --name myResourceGroup
Azure CLI exam az-104

Reference: Microsoft Documentation

Return to AZ-104 Tutorial

Menu