Back to Blog

Run containers with Azure Container Instances

Complete tutorial about az container create in Azure CLI. Learn CPU/memory, env vars/secrets, volumes, VNet integration.

Run containers with Azure Container Instances

Run Containers with Azure Container Instances

Azure Container Instances (ACI) is a powerful service that simplifies the deployment of containerized applications without the need to manage underlying infrastructure. With ACI, you can run containers in seconds, making it ideal for scenarios such as microservices, batch jobs, and event-driven applications. In this tutorial, we will focus on the az container create command, which is pivotal for launching container instances in Azure.

We'll explore how to leverage ACI to run containers while controlling CPU and memory resources, setting environment variables and secrets, using volumes for persistent storage, and integrating with virtual networks. By the end of this tutorial, you'll have a comprehensive understanding of how to effectively manage containers using Azure CLI.

Prerequisites

Before you begin, ensure you have the following:

  • Azure CLI: Install Azure CLI version 2.0.29 or later. You can check your version with az --version. If you need to install or upgrade, follow the instructions here.
  • Azure Subscription: You need an active Azure subscription. If you don't have one, you can create a free account here.
  • Permissions: Ensure you have permissions to create resource groups and container instances.
  • Authentication: Set up authentication to your Azure account using az login.

Fundamental Concepts

  • Container: A lightweight, portable software unit that includes everything needed to run an application.
  • Container Instance: A single instance of a running container in Azure.
  • Resource Group: A container that holds related resources for an Azure solution.
  • Environment Variables: Key-value pairs that define configuration settings for your container.
  • Secrets: Sensitive information (like passwords) that should not be hard-coded in applications.
  • Volumes: Storage that persists beyond the lifecycle of a container.
  • VNet Integration: The ability to deploy container instances within an Azure Virtual Network for secure communication.

Command Syntax

The general syntax for creating a container instance is as follows:

az container create --resource-group <resource-group-name> --name <container-name> --image <image-name> [--cpu <value>] [--memory <value>] [--environment-variables <key=value>] [--secrets <key=value>] [--dns-name-label <label>] [--ports <port>] [--os-type <os-type>] [--volume-mounts <mounts>] [--vnet <vnet-name>]

Parameters Table

Parameter Description
--resource-group The name of the resource group.
--name The name of the container instance.
--image The Docker image to use.
--cpu Number of CPU cores to allocate.
--memory Amount of memory to allocate (in GB).
--environment-variables Environment variables for the container.
--secrets Secrets for the container.
--dns-name-label DNS name for public access.
--ports Ports to expose.
--os-type The OS type (Linux or Windows).
--volume-mounts Mount points for persistent storage.
--vnet The name of the VNet to integrate with.

Practical Examples

1. Create a Basic Container Instance

az container create --resource-group myResourceGroup --name myContainer --image mcr.microsoft.com/azuredocs/aci-helloworld --dns-name-label aci-demo --ports 80

This command creates a basic container instance running a hello world application.

2. Specify CPU and Memory

az container create --resource-group myResourceGroup --name myContainer --image mcr.microsoft.com/azuredocs/aci-helloworld --cpu 1 --memory 1.5

Allocate 1 CPU core and 1.5 GB of memory to the container.

3. Set Environment Variables

az container create --resource-group myResourceGroup --name myContainer --image myimage --environment-variables ENV_VAR1=value1 ENV_VAR2=value2

Set environment variables for the application running inside the container.

4. Use Secrets

az container create --resource-group myResourceGroup --name myContainer --image myimage --secrets secret1=value1 secret2=value2

Pass sensitive information to the container securely.

5. Mount a Volume

az container create --resource-group myResourceGroup --name myContainer --image myimage --volume-mounts mountPath=/mnt/data --azure-file-share <file-share-name>

Mount an Azure File Share as a volume to the container.

6. Integrate with a Virtual Network

az container create --resource-group myResourceGroup --name myContainer --image myimage --vnet myVnet --subnet mySubnet

Deploy the container within a specified VNet and subnet.

7. Create a Multi-Container Group

az container create --resource-group myResourceGroup --name myContainerGroup --file myContainerGroup.yml

Use a YAML file to define and create a multi-container group.

8. Set Up Autoscaling (Advanced)

az container create --resource-group myResourceGroup --name myContainer --image myimage --cpu 2 --memory 4 --restart-policy OnFailure

Set a restart policy for the container, allowing it to restart on failure.

Real-World Use Cases

1. Microservices Architecture

In a microservices architecture, ACI can be used to deploy individual microservices as separate container instances, allowing for easy scaling and management.

2. Batch Processing Jobs

ACI is ideal for running batch jobs where containers can be spun up to process tasks in parallel, such as image processing or data analysis.

3. Event-Driven Applications

Integrate ACI with Azure Functions to run containers in response to events, such as HTTP requests or messages in a queue.

Best Practices

  1. Use Resource Limits: Always specify CPU and memory limits to avoid resource contention and ensure optimal performance.
  2. Secure Secrets: Store sensitive information in Azure Key Vault and reference it in your containers instead of hardcoding.
  3. Monitor Performance: Utilize Azure Monitor to track the performance and health of your containers.
  4. Use Networking Wisely: Integrate ACI with VNets for secure communication between services.
  5. Automate Deployments: Use CI/CD pipelines to automate container deployments and updates.

Common Errors

1. DNS Name Label Not Available

Error: DNS name label not available

Cause: The specified DNS name is already in use.

Solution: Choose a different DNS name label.

2. Insufficient Resource Quota

Error: Insufficient quota for resource

Cause: The requested resources exceed the available quota for your subscription.

Solution: Reduce the requested resources or request an increase in your subscription's quota.

3. Image Not Found

Error: Image not found

Cause: The specified image does not exist in the repository.

Solution: Verify the image name and ensure it is correctly spelled and available.

4. Failed to Start Container

Error: Container failed to start

Cause: The application inside the container crashed or failed to launch.

Solution: Check the container logs for errors using az container logs --name <container-name> --resource-group <resource-group-name>.

Related Commands

Command Description
az container list List container instances.
az container show Show details about a container.
az container logs Fetch logs from a running container.
az container delete Delete a container instance.
az group create Create a resource group.

Automation Script

Here’s a complete bash script that automates the creation of a container instance with environment variables and a mounted volume.

#!/bin/bash

# Variables
RESOURCE_GROUP="myResourceGroup"
CONTAINER_NAME="myContainer"
IMAGE_NAME="myimage"
MEMORY="1.5"
CPU="1"
DNS_NAME_LABEL="aci-demo"
FILE_SHARE_NAME="myfileshare"

# Create Resource Group
az group create --name $RESOURCE_GROUP --location eastus

# Create Container Instance
az container create --resource-group $RESOURCE_GROUP --name $CONTAINER_NAME --image $IMAGE_NAME \
--memory $MEMORY --cpu $CPU --dns-name-label $DNS_NAME_LABEL --ports 80 \
--environment-variables ENV_VAR1=value1 ENV_VAR2=value2 \
--volume-mounts mountPath=/mnt/data --azure-file-share $FILE_SHARE_NAME

echo "Container instance '$CONTAINER_NAME' created successfully!"

Conclusion

In this tutorial, we covered the essentials of using Azure Container Instances with the az container create command. You learned about creating container instances, setting resource limits, using environment variables and secrets, mounting volumes, and integrating with virtual networks. By mastering these commands and concepts, you can effectively manage your containerized applications in Azure.

Next Steps

  • Explore more advanced features of ACI, such as multi-container groups and integration with Azure Kubernetes Service.
  • Experiment with Azure CLI and Azure Portal to get comfortable using both interfaces.
  • Consider using Azure Container Registry to store and manage your container images.

References