Azure Container Instance Management
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
- breaking Starting from version 9.0.0, the `ContainerInstanceManagementClient` no longer accepts a `base_url` parameter. Authentication methods have also shifted from `msrestazure` credentials (e.g., `ServicePrincipalCredentials`) to `azure-identity` (e.g., `DefaultAzureCredential`).
- breaking In version 9.0.0 and later, the `create_or_update` method for `ContainerGroups` changed parameter names. Specifically, `resource_group_name` was renamed to `resource_group_name_param` for clarity and consistency across Azure SDKs.
- gotcha Many Azure management operations, especially those that create or update resources (e.g., `begin_create_or_update`, `begin_delete`), are long-running operations (LROs). These methods return a poller object, not the final resource directly.
- gotcha Resource group operations (creating container instances) require the specified resource group to already exist in Azure. If it doesn't, the operation will fail.
Install
-
pip install azure-mgmt-containerinstance azure-identity
Imports
- ContainerInstanceManagementClient
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
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.")