In this article we will discuss step by step creation and deployment of a docker containers in the Azure Resource Group using Azure CLI. Later we will also cover how we can start, stop, restart, or delete the container group that we created, all this is can be alternatively performed using the Graphical User Interface (GUI) of the Azure portal, but this piece will only focus on using Azure CLI.

What is Docker and Containers ^

It is prerequisite that readers have some basic understanding of Docker and containers in general, but for sake of doing it, following are simple and short definitions of each:

  • Docker: Docker is a platform or a tool which can be used to create and deploy the applications using containers.
  • Containers: A Container is an encapsulated unit of an application and all its dependencies such that they are independent of underlying systems, or in simpler words platform independent.

What is Azure CLI ^

Azure CLI or is a command line interface available in Azure Cloud Shell in the Azure portal that can be used to create, delete and manage the Azure resources. One of the biggest advantages of using the Azure CLI to deploy Docker containers is that you don’t need to install docker in cloud shell as it comes pre-installed.

Let us start deploying a docker container to Azure using Azure CLI

Creating a Resource Group ^

We will be deploying an Azure Container instance and hence we would require a resource group in which can hold these instances. To create a resource group, please run following command in PowerShell on Azure Cloud Shell:

az group create --name RG-Container --location CentralIndia

and the result will look like below attached screenshot

Create resource group in Azure

Create resource group in Azure

To view the resource group, you can also run the following command with show keyword.

az group show --name RG-Container -o table
View resource group

View resource group

Creating a container/container instance ^

Once a resource group is created where we can deploy an azure container instance, lets proceed with the next step of creating a container. We will be using following attributes while creating azure container instance:

  • --resource-group : to specify the resource group in which the azure container instance will be deployed
  • --name : to specify the name of the azure container instance
  • --image : to specify the docker container image
  • --dns-name-label : to specify the DNS name label so as the web app can be accessed publicly over the internet and this has to be unique within the region of azure where we are creating the container instance.
  • --ports : to specify the port on which the web app will be available to communicate.

In this example we will be using public image from Docker hub: nginx , which is a free open-source web server.

For the convenience let’s put the name of resource group and the container in the variables and run the following in Azure cloud shell.

$resourcegroup = 'RG-Container'
$ContainerName = 'democontainer’

Once that is done, we will run the following command to create a container instance using Azure CLI:

az container create `
    --resource-group $resourcegroup `
    --name $containerName `
    --image nginx `
    --dns-name-label $containerName `
    --port 80

After some time once the command is executed successfully, you will see the results as below:

Create container using Azure CLI

Create container using Azure CLI

And there would be lot more information mentioned in it. The first thing we will need to check in the results is provisioning state. If it shows as succeeded it means the container is successfully created and deployed in the mentioned resource group. The provisioning status result will look as demonstrated in the following image:

Verify container provisioning was successful

Verify container provisioning was successful

We can also check the provisioning state, IP and FQDN of the container using following command:

az container show `
        --resource-group $resourcegroup `
        --name $ContainerName `
        --query "{FQDN:ipAddress.fqdn,IPAddress:ipAddress.ip,ProvisioningState:provisioningState}" `
        --out table
Show containers deployed using Azure CLI

Show containers deployed using Azure CLI

Now let us quickly verify if the web server was successfully deployed in a docker container to Azure by coping the FQDN retrieved, as demonstrated in the above example, by simply using the URL in any browser of your choice. In our example the FQDN is ‘democontainer.centralindia.azurecontainer.io’ and once we open it, the resulting page should look like in the following example:

Verify the web server using the FQDN of of the Azure container

Verify the web server using the FQDN of of the Azure container

Furthermore, the same can be tried using the public IP address retrieved from the above results of “az container show”. If you get the similar page like above means your web app and container instance is successfully deployed.

Now as the container is deployed, let’s look at some examples on how we can manage them using Azure CLI

Stopping and Starting containers in a container group ^

To stop the containers in a resource group, we simply use ‘container stop’ as demonstrated in the following example:

az container stop `
    --resource-group $resourcegroup `
    --name $containername

and to verify the results, we can use following command and check the status which would be showing stopped:

az container show `
    --resource-group $resourcegroup `
    --name $containername `
    --out table
Checking the stopped Azure Container

Checking the stopped Azure Container

To start it again, we can use following command with ‘start’ keyword:

az container start `
    --resource-group $resourcegroup `
    --name $containername

While it would be processing the request of start you will see following message on your screen:

Starting a container using Azure CLI

Starting a container using Azure CLI

Once started you can run the “az container show” command again with the output in table format which will show you the status as running. While we have checked how we can stop and start all the containers in a container group let’s see how we can restart it directly with one single command instead of stopping and starting:

az container restart `
    --resource-group $resourcegroup `
    --name $containername

Again, while it would be processing the request CLI will show you the message as shown in below image:

Restarting a container using Azure CLI

Restarting a container using Azure CLI

Once you are done with the work and simply want to get rid of container instances you have deployed, then you can simply delete a container using the following command:

az container delete `
    --resource-group $resourcegroup `
    --name $containername

As soon as we will hit enter it will prompt us to review our action and agree or cancel the command

Deleting a container using Azure CLI

Deleting a container using Azure CLI

Once the deletion is completed, you can review if the concerned container group is existing or not through the same az container show command.

Subscribe to 4sysops newsletter!

Conclusion ^

Pulling an container image from Docker hub and then deploying a container using the same image in Azure CLI is not only simple as just running few Azure CLI commands but, it is at the same time effective since you get Docker preinstalled in Azure cloud shell where we are using the command line interface. More importantly you get all the goodness of Azure cloud natively like Azure portal-based management, configuration and inbuild monitoring.

0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account