Azure Container Instance Management

10.1.0 · active · verified Thu Apr 09

The `azure-mgmt-containerinstance` library provides a client for managing Azure Container Instances, allowing programmatic creation, update, and deletion of container groups and related resources. It is part of the larger Azure SDK for Python and is currently at version 10.1.0, with frequent updates aligning with Azure service API changes and broader SDK release cycles.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with `DefaultAzureCredential` and create a simple Azure Container Instance (ACI) using the `ContainerInstanceManagementClient`. It sets up a container group with a single public-facing container running a hello-world image. Remember to set your Azure Subscription ID, Resource Group Name, and Location in environment variables or directly in the script.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.containerinstance import ContainerInstanceManagementClient

# --- Environment Variables (Replace with your actual values or ensure they are set) ---
SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")
RESOURCE_GROUP_NAME = os.environ.get("AZURE_RESOURCE_GROUP", "my-aci-rg")
LOCATION = os.environ.get("AZURE_LOCATION", "eastus")
CONTAINER_GROUP_NAME = os.environ.get("AZURE_ACI_NAME", "my-aci-container-group")

if SUBSCRIPTION_ID == "YOUR_SUBSCRIPTION_ID":
    raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable.")

# Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)
credential = DefaultAzureCredential()

# Create a ContainerInstanceManagementClient
client = ContainerInstanceManagementClient(credential, SUBSCRIPTION_ID)

# Define the container group
container_group_body = {
    "location": LOCATION,
    "containers": [
        {
            "name": "mycontainer",
            "image": "mcr.microsoft.com/azuredocs/aci-helloworld",
            "resources": {
                "requests": {
                    "cpu": 1.0,
                    "memory_in_gb": 1.5
                }
            },
            "ports": [{"port": 80}]
        }
    ],
    "os_type": "Linux",
    "ip_address": {
        "type": "Public",
        "ports": [
            {
                "protocol": "TCP",
                "port": 80
            }
        ]
    }
}

print(f"Creating container group '{CONTAINER_GROUP_NAME}' in resource group '{RESOURCE_GROUP_NAME}'...")

# Create or update the container group (this returns a poller object)
lro_poller = client.container_groups.begin_create_or_update(
    resource_group_name=RESOURCE_GROUP_NAME,
    container_group_name=CONTAINER_GROUP_NAME,
    container_group=container_group_body
)

# Wait for the operation to complete and get the result
container_group = lro_poller.result()

print(f"Container group '{container_group.name}' created/updated successfully.")
print(f"IP Address: {container_group.ip_address.ip}")

# To delete the container group (optional)
# print(f"Deleting container group '{CONTAINER_GROUP_NAME}'...")
# delete_poller = client.container_groups.begin_delete(
#     resource_group_name=RESOURCE_GROUP_NAME,
#     container_group_name=CONTAINER_GROUP_NAME
# )
# delete_poller.result()
# print(f"Container group '{CONTAINER_GROUP_NAME}' deleted.")

view raw JSON →