Docker Container With Azure Container Instance

Introduction

In this article, we will explore how to create a containerized application in Azure and how we can reach it out from the outside world.

Benefits of using container instance

  • Azure container instance can be launched in few seconds without delay
  • Pricing will be charged per second when the instance is running
  • More secure and clear isolation between the application and platform
  • Flexibility and control on container CPU cores and memory
  • Ability to create persistent storage for containers with Azure File Share
  • Work with the same APIs for both Linux and Windows VMs

After understanding the benefits of Azure Container, let's understand how to create an Azure Container Instance. You need the below resources before creating an Azure container instance.

  • Azure Subscription 
  • Azure Resource Group

Let's create a container instance in Azure.

DNS_NAME_LABEL=aci-demo-321
az container create --resource-group acr-d-rg --name mycontainer --image mcr.microsoft.com/azuredocs/helloworld --ports 80 --dns-name-label $DNS_NAME_LABEL --location westeurope

Once the container is created, we can browse the container using the FQDN.

Docker Container with Azure Container Instance

Container Restart Policy

Azure Container has 3 different restart policies.

  • Always
  • Never
  • On Failure

Set Environment Variables 

We can use Azure Powershell/Azure CLI or Azure Portal to set and create environment variables for Azure container instances. We can preconfigure the environment variables to avoid displaying sensitive information.

For example, we will create an Azure Cosmos DB instance and use the environment variable to pass on the connection information.

Step 1: Create Azure Cosmos DB

COSMOS_DB_NAME=aci-cosmos-db-21
COSMOS_DB_ENDPOINT=$(az cosmosdb create --resource-group acr-d-rg --name $COSMOS_DB_NAME --query documentEndpoint --output tsv)

Step 2: Deploy Cosmos DB to container

az container create --resource-group acr-d-rg  -name aci-d --image mcr.microsoft.com/azuredocs/azure-vote-front:cosmosdb --ip-address Public --location westeurope --environment-variables COSMOS_DB_ENDPOINT=$COSMOS_DB_ENDPOINT COSMOS_DB_MASTERKEY=$COSMOS_DB_MASTERKEY

Data Volume in Azure Container Instance

By default, Azure container instances are stateless. Due to which we can lot all the data if the container fails. In order to avoid such issues, we can also mount a volume from an external store.

Steps to create persistent storage using Azure File Storage is as follows,

Create Storage Account

STORAGE_ACCOUNT_NAME=mystorageaccount231
az storage account create --resource-group acr-d-rg --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --location westeurope

Create an environment variable for storage account key

export AZURE_STORAGE_CONNECTION_STRING=$(az storage account show-connection-string --resource-group acr-d-rg --name $STORAGE_ACCOUNT_NAME --output tsv)

Create a fileshare,

az storage share create --name aci

Deploy a container and mount fileshare,

az container create --resource-group acr-d-rg --name aci --image mcr.microsoft.com/azuredocs/aci-hellofiles --location westeurope --ports 80 --ip-address Public --azure-file-volume-account-name $STORAGE_ACCOUNT_NAME --azure-file-volume-account-key $STORAGE_KEY --azure-file-volume-share-name aci-share --azure-file-volume-mount-path /aci/logs/

After understanding the container creation and deployment of the Azure cosmos DB container, Let's also explore how to troubleshoot Azure Container Instances. We will explore about below points,

  • Get Container Logs
    az container logs --resource-group acr-d-rg --name aci-demo
     
  • Visualize container events
    az container attach --resource-group acr-d-rg --name aci-demo

Conclusion

In this article, we explored the azure container instance and how to create and deploy Azure services to the Azure container instance.