Azure Container Instances
Azure Container Instances (ACI) provides fast deployment of Linux containers without managing virtual machines. Deploy containers with public IP addresses, custom resource allocations, and private registry support.
List Containers
Retrieve all Azure Container Instances across subscriptions and resource groups.
Endpoint
GET /api/azure/containers
Authentication
Requires access_token in session.
Response
{
"value" : [
{
"name" : "my-container" ,
"resourceGroup" : "my-rg" ,
"location" : "westeurope" ,
"status" : "Running" ,
"image" : "nginx:latest" ,
"subscriptionId" : "12345678-1234-1234-1234-123456789abc"
}
]
}
Implementation
The list operation iterates through all subscriptions and resource groups:
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.subscription import SubscriptionClient
def list_containers ():
credential = FlaskCredential()
sub_client = SubscriptionClient(credential)
items = []
for sub in sub_client.subscriptions.list():
subscription_id = sub.subscription_id
container_client = ContainerInstanceManagementClient(
credential, subscription_id
)
resource_client = ResourceManagementClient(
credential, subscription_id
)
for rg in resource_client.resource_groups.list():
for cn in container_client.container_groups.list_by_resource_group(rg.name):
details = container_client.container_groups.get(rg.name, cn.name)
items.append({
"name" : cn.name,
"resourceGroup" : rg.name,
"location" : cn.location,
"status" : details.instance_view.state,
"image" : cn.containers[ 0 ].image if cn.containers else "unknown" ,
"subscriptionId" : subscription_id
})
return jsonify({ "value" : items})
Create Container
Deploy a new container instance with custom configurations.
Endpoint
POST /api/azure/containers
Request Body
{
"subscriptionId" : "12345678-1234-1234-1234-123456789abc" ,
"rgName" : "my-resource-group" ,
"location" : "westeurope" ,
"cnName" : "my-container" ,
"image" : "nginx:latest" ,
"cpu" : 1.0 ,
"memory" : 1.5 ,
"port" : 80 ,
"registry" : {
"server" : "myregistry.azurecr.io" ,
"username" : "myusername" ,
"password" : "mypassword"
}
}
Parameters
Azure subscription ID where the container will be created
Resource group name for the container
location
string
default: "westeurope"
Azure region for deployment (e.g., westeurope, eastus)
Container image (e.g., nginx:latest, myregistry.azurecr.io/myapp:v1)
Number of CPU cores (0.1 to 4.0)
Memory allocation in GB (0.1 to 16.0)
Private registry credentials (optional)
server: Registry server URL
username: Registry username
password: Registry password
Response
{
"message" : "Kontener 'my-container' utworzony pomyślnie" ,
"containerId" : "/subscriptions/.../resourceGroups/my-rg/providers/Microsoft.ContainerInstance/containerGroups/my-container"
}
Implementation
from azure.mgmt.containerinstance.models import (
ContainerGroup,
Container,
ResourceRequests,
ResourceRequirements,
OperatingSystemTypes,
IpAddress,
Port,
ContainerPort,
ContainerGroupNetworkProtocol,
ImageRegistryCredential
)
def create_container ():
data = request.get_json()
subscription_id = data.get( "subscriptionId" )
rg_name = data.get( "rgName" )
location = data.get( "location" , "westeurope" )
cn_name = data.get( "cnName" )
image = data.get( "image" )
cpu = data.get( "cpu" , 1.0 )
memory = data.get( "memory" , 1.5 )
port = data.get( "port" , 80 )
registry = data.get( "registry" )
credential = FlaskCredential()
client = ContainerInstanceManagementClient(credential, subscription_id)
# Define resource requirements
container_resource_requests = ResourceRequests(
cpu = cpu,
memory_in_gb = memory
)
container_resource_requirements = ResourceRequirements(
requests = container_resource_requests
)
# Configure container
container = Container(
name = cn_name,
image = image,
resources = container_resource_requirements,
ports = [ContainerPort( port = port)]
)
# Configure public IP address
ip_address = IpAddress(
ports = [Port( protocol = ContainerGroupNetworkProtocol. TCP , port = port)],
type = "Public"
)
# Add registry credentials if provided
registry_credentials = []
if registry:
registry_credentials.append(ImageRegistryCredential(
server = registry.get( "server" ),
username = registry.get( "username" ),
password = registry.get( "password" )
))
# Create container group
group = ContainerGroup(
location = location,
containers = [container],
os_type = OperatingSystemTypes. LINUX ,
restart_policy = "Always" ,
ip_address = ip_address,
image_registry_credentials = registry_credentials
)
result = client.container_groups.begin_create_or_update(
resource_group_name = rg_name,
container_group_name = cn_name,
container_group = group
).result()
return jsonify({
"message" : f "Kontener ' { cn_name } ' utworzony pomyślnie" ,
"containerId" : result.id
}), 200
Container Configuration Details
Resource Allocation:
CPU cores are specified as floating-point numbers (e.g., 0.5, 1.0, 2.0)
Memory is specified in gigabytes
Azure charges based on allocated resources, not actual usage
Operating System:
Only Linux containers are supported in this implementation
Windows containers require OperatingSystemTypes.WINDOWS
Restart Policy:
Always: Restart container on failure (default)
OnFailure: Restart only if container exits with error
Never: Never restart the container
Network Configuration:
Public IP addresses are automatically assigned
Multiple ports can be exposed
Supports TCP and UDP protocols
Restart Container
Restart a running container instance.
Endpoint
POST /api/azure/containers/restart
Request Body
{
"subscriptionId" : "12345678-1234-1234-1234-123456789abc" ,
"resourceGroup" : "my-rg" ,
"containerName" : "my-container"
}
Parameters
Container group name to restart
Response
{
"message" : "Kontener 'my-container' został zrestartowany"
}
Implementation
def restart_container ():
data = request.get_json()
subscription_id = data.get( "subscriptionId" )
resource_group = data.get( "resourceGroup" )
container_name = data.get( "containerName" )
credential = FlaskCredential()
client = ContainerInstanceManagementClient(credential, subscription_id)
client.container_groups.begin_restart(
resource_group,
container_name
).wait()
return jsonify({
"message" : f "Kontener ' { container_name } ' został zrestartowany"
}), 200
The restart operation is asynchronous. The API returns after initiating the restart, but the container may take a few seconds to fully restart.
Delete Container
Delete a container instance and release all resources.
Endpoint
DELETE /api/azure/containers
Request Body
{
"subscriptionId" : "12345678-1234-1234-1234-123456789abc" ,
"resourceGroup" : "my-rg" ,
"containerName" : "my-container"
}
Parameters
Container group name to delete
Response
{
"message" : "Kontener 'my-container' został usunięty"
}
Implementation
def delete_container ():
data = request.get_json()
subscription_id = data.get( "subscriptionId" )
resource_group = data.get( "resourceGroup" )
container_name = data.get( "containerName" )
credential = FlaskCredential()
client = ContainerInstanceManagementClient(credential, subscription_id)
client.container_groups.begin_delete(
resource_group,
container_name
).wait()
return jsonify({
"message" : f "Kontener ' { container_name } ' został usunięty"
}), 200
Deleting a container is permanent and cannot be undone. Ensure you have backups of any important data before deletion.
Error Handling
Common error responses:
Unauthorized (401)
{
"error" : "Unauthorized"
}
The access token is missing or invalid. Re-authenticate to obtain a valid token.
Bad Request (400)
{
"error" : "Brak wymaganych danych"
}
Required parameters are missing from the request.
Server Error (500)
{
"error" : "The subscription is not registered to use namespace 'Microsoft.ContainerInstance'"
}
The Azure subscription does not have the Container Instances resource provider registered.
Best Practices
Resource Sizing
Start with minimal resources (0.5 CPU, 0.5 GB memory) and scale up as needed
Monitor CPU and memory usage to right-size containers
Consider Azure Container Apps for production workloads requiring auto-scaling
Image Management
Use specific image tags instead of latest for production
Store images in Azure Container Registry for better performance
Enable vulnerability scanning in ACR
Use managed identity instead of registry passwords when possible
Security
Store registry credentials in Azure Key Vault
Use private endpoints for ACR access
Implement network security groups for container traffic
Regularly update base images to patch vulnerabilities
Cost Optimization
Delete unused containers to avoid charges
Use restart policy OnFailure or Never for batch jobs
Consider Azure Container Apps for better price/performance
Use spot instances for fault-tolerant workloads
Container Monitoring Monitor Azure Container Instances with metrics and logs
Container Alerts Configure alerts for container health and performance