Back to Blog

Deploying Containers with Azure Container Instances

Complete tutorial about az container create in Azure CLI. Learn ACI, containers, quick deploy.

Deploying Containers with Azure Container Instances

Deploying Containers with Azure Container Instances

Introduction

Azure Container Instances (ACI) is a powerful platform that simplifies the deployment of containerized applications in the cloud without the overhead of managing virtual machines. The az container create command is at the heart of ACI, enabling developers to quickly deploy containers in a serverless manner. This command is significant for developers focusing on microservices architecture and rapid application deployments, as it supports various container images from public registries like Docker Hub or Azure Container Registry.

ACI is ideal for scenarios where quick deployment and scaling of applications are crucial, such as web applications, background jobs, and task automation. Using ACI, developers can run Linux or Windows containers with ease, making it an essential tool for modern cloud-based application development.

Prerequisites

Before you begin, ensure you have the following:

  • Azure CLI installed on your machine. (You can download it here).
  • An active Azure subscription. You can create a free account if you don’t have one here.
  • Sufficient permissions to create resources in your Azure subscription.
  • Authentication to Azure CLI using az login.

Fundamental Concepts

  • Container: A lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
  • Azure Container Instances (ACI): A service that allows you to run containers on demand without managing the underlying infrastructure.
  • Resource Group: A container that holds related resources for an Azure solution. It provides a way to manage and organize resources.
  • DNS Name Label: A unique name assigned to your container instance to access it through a public IP address.

Command Syntax

The syntax for the az container create command is as follows:

az container create --resource-group <resource-group-name> --name <container-name> --image <image-name> [--cpu <cpu-cores>] [--memory <memory-in-gb>] [--dns-name-label <dns-name-label>] [--ports <port-numbers>] [--os-type <os-type>]

Parameters Table

Parameter Description
--resource-group -g The name of the resource group.
--name -n The name of the container instance.
--image -i The container image to deploy (e.g., nginx:latest).
--cpu The number of CPU cores required (e.g., 1).
--memory The amount of memory required (in GB).
--dns-name-label A unique DNS name for public access.
--ports A space-separated list of ports to expose.
--os-type The operating system type (Linux or Windows).

Practical Examples

Example 1: Create a Simple Container Instance

Deploy a basic Nginx container:

az container create --resource-group myResourceGroup --name mynginx --image nginx --dns-name-label mynginxapp --ports 80

This command creates a container instance running Nginx, accessible via a public DNS name.

Example 2: Specify CPU and Memory

Deploy a container with specific resource requirements:

az container create --resource-group myResourceGroup --name myapp --image myapp:latest --cpu 1 --memory 1.5 --dns-name-label myappdemo --ports 8080

This allocates 1 CPU core and 1.5 GB of memory for the application container.

Example 3: Environment Variables

Pass environment variables to the container:

az container create --resource-group myResourceGroup --name myapp --image myapp:latest --environment-variables ENV=production DEBUG=false

Use this to configure your application with environment-specific settings.

Example 4: Use a Private Azure Container Registry

Login to your Azure Container Registry and deploy:

az container create --resource-group myResourceGroup --name myapp --image <myregistry>.azurecr.io/myapp --registry-login-server <myregistry>.azurecr.io --registry-username <username> --registry-password <password>

This command uses a private registry for image deployment, enhancing security.

Example 5: Expose Multiple Ports

Expose multiple ports for the application:

az container create --resource-group myResourceGroup --name myapp --image myapp:latest --ports 80 443

Ideal for web applications that need to handle both HTTP and HTTPS traffic.

Example 6: Deploy with a Custom DNS Name

Assign a custom DNS label:

az container create --resource-group myResourceGroup --name myapp --image myapp:latest --dns-name-label mycustomapp

This makes the application accessible at http://mycustomapp.<region>.azurecontainer.io.

Example 7: Using YAML for Deployment

Define your deployment in a YAML file:

apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
  - name: myapp
    image: myapp:latest
    ports:
    - containerPort: 80

Deploy using:

az container create --resource-group myResourceGroup --file myapp.yaml

YAML files provide a clean way to manage complex deployments.

Example 8: Create a Container in a Virtual Network

Deploy a container in an existing virtual network:

az container create --resource-group myResourceGroup --name myapp --image myapp:latest --vnet myVNet --subnet mySubnet

This allows for secure communication between containers and other resources in the virtual network.

Real-World Use Cases

Scenario 1: Web Application Hosting

A company needs to host a web application that scales with traffic. Using ACI, they can deploy their application containers quickly and expose them to the internet, allowing users to access them without worrying about underlying infrastructure.

Scenario 2: Background Job Processing

For a data processing task that runs nightly, using ACI allows the company to spin up containers only when needed. This saves costs and resources, as the containers are not running continuously.

Scenario 3: Microservices Architecture

In a microservices architecture, different components of an application can be deployed as separate containers. ACI enables developers to deploy these components independently, allowing for faster iterations and scalability.

Best Practices

  1. Resource Limitation: Always specify CPU and memory limits to avoid unexpected costs.
  2. Use Managed Identity: For secure access to Azure services, leverage managed identities instead of handling credentials within your containers.
  3. Monitor Performance: Utilize Azure Monitor to track the performance and health of your container instances.
  4. Use DNS Names Wisely: Choose unique and meaningful DNS labels for easier access and identification.
  5. Automate Deployments: Use scripts or CI/CD pipelines to automate the deployment of new container instances.

Common Errors

  1. Error: "DNS name label not available."

    • Cause: The DNS name label is already taken.
    • Solution: Choose a different, unique DNS name label.
  2. Error: "Resource group not found."

    • Cause: The specified resource group doesn’t exist.
    • Solution: Create the resource group using az group create.
  3. Error: "Image not found."

    • Cause: The specified image is not available in the registry.
    • Solution: Verify the image name and ensure it exists in the specified registry.
  4. Error: "Insufficient resources."

    • Cause: The specified CPU or memory exceeds the available resources in the region.
    • Solution: Lower the resource requests or try a different region.

Related Commands

Command Description
az container list List all container instances.
az container show Show details of a specific container.
az container delete Delete a container instance.
az group create Create a resource group.

Automation Script

Here's a simple bash script to automate the deployment of a container instance:

#!/bin/bash

RESOURCE_GROUP="myResourceGroup"
CONTAINER_NAME="myapp"
IMAGE_NAME="myapp:latest"
DNS_NAME_LABEL="myappdemo"

# 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 --dns-name-label $DNS_NAME_LABEL --ports 80

echo "Container instance $CONTAINER_NAME is being created..."

Conclusion

Azure Container Instances provide a powerful and flexible way to deploy containers quickly and efficiently without the management overhead of virtual machines. By mastering the az container create command, developers can leverage the benefits of containerization in their applications, from microservices to simple web apps.

Next Steps

  1. Experiment with more complex deployments using YAML files.
  2. Explore integrating ACI with Azure Kubernetes Service for orchestration.
  3. Monitor your container instances using Azure Monitor for insights into performance and resource usage.

References