Microsoft Azure Container Instance Client Library for Python providing comprehensive management capabilities for containerized applications in Azure cloud.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Container operations provide direct interaction with individual containers within container groups, including log retrieval, command execution, and container attachment.
def list_logs(
resource_group_name: str,
container_group_name: str,
container_name: str,
**kwargs
) -> Logs:
"""
Retrieve logs from a specific container.
Args:
resource_group_name (str): Name of the resource group
container_group_name (str): Name of the container group
container_name (str): Name of the container
tail (int, optional): Number of lines to tail from the end of the logs
timestamps (bool, optional): Include timestamps in the log output
Returns:
Logs: Container logs with content property containing log text
Example:
# Get all logs
logs = client.containers.list_logs(
resource_group_name="my-resource-group",
container_group_name="my-container-group",
container_name="web-server"
)
print(logs.content)
# Get last 100 lines with timestamps
recent_logs = client.containers.list_logs(
resource_group_name="my-resource-group",
container_group_name="my-container-group",
container_name="web-server",
tail=100,
timestamps=True
)
print(recent_logs.content)
"""def execute_command(
resource_group_name: str,
container_group_name: str,
container_name: str,
container_exec_request: ContainerExecRequest,
**kwargs
) -> ContainerExecResponse:
"""
Execute a command inside a running container.
Args:
resource_group_name (str): Name of the resource group
container_group_name (str): Name of the container group
container_name (str): Name of the container
container_exec_request (ContainerExecRequest): Command execution request
Returns:
ContainerExecResponse: Response containing WebSocket URI and password for command execution
Example:
from azure.mgmt.containerinstance.models import (
ContainerExecRequest, ContainerExecRequestTerminalSize
)
# Execute a simple command
exec_request = ContainerExecRequest(
command=["ls", "-la", "/app"]
)
exec_response = client.containers.execute_command(
resource_group_name="my-resource-group",
container_group_name="my-container-group",
container_name="web-server",
container_exec_request=exec_request
)
print(f"WebSocket URI: {exec_response.web_socket_uri}")
# Execute interactive bash with terminal size
interactive_request = ContainerExecRequest(
command=["/bin/bash"],
terminal_size=ContainerExecRequestTerminalSize(rows=24, cols=80)
)
interactive_response = client.containers.execute_command(
resource_group_name="my-resource-group",
container_group_name="my-container-group",
container_name="web-server",
container_exec_request=interactive_request
)
"""def attach(
resource_group_name: str,
container_group_name: str,
container_name: str,
**kwargs
) -> ContainerAttachResponse:
"""
Attach to a container's main process for interactive access.
Args:
resource_group_name (str): Name of the resource group
container_group_name (str): Name of the container group
container_name (str): Name of the container
Returns:
ContainerAttachResponse: Response containing WebSocket URI and password for attachment
Example:
# Attach to container
attach_response = client.containers.attach(
resource_group_name="my-resource-group",
container_group_name="my-container-group",
container_name="interactive-container"
)
print(f"Attach WebSocket URI: {attach_response.web_socket_uri}")
# Use the WebSocket URI to establish connection for interactive access
"""import time
from azure.mgmt.containerinstance.models import ContainerExecRequest
def monitor_container_health(client, resource_group, container_group, container_name):
"""Monitor container health through logs and commands."""
# Check recent logs for errors
logs = client.containers.list_logs(
resource_group_name=resource_group,
container_group_name=container_group,
container_name=container_name,
tail=50,
timestamps=True
)
if "ERROR" in logs.content or "FAILED" in logs.content:
print("Errors detected in logs:")
print(logs.content)
# Execute health check command
health_check = ContainerExecRequest(
command=["curl", "-f", "http://localhost:8080/health"]
)
health_response = client.containers.execute_command(
resource_group_name=resource_group,
container_group_name=container_group,
container_name=container_name,
container_exec_request=health_check
)
# Process health check response through WebSocket connection
print(f"Health check WebSocket: {health_response.web_socket_uri}")
# Usage
monitor_container_health(
client=client,
resource_group="production-rg",
container_group="web-app-cg",
container_name="web-server"
)def run_database_migration(client, resource_group, container_group, container_name):
"""Execute database migration scripts in a container."""
# Run migration command
migration_request = ContainerExecRequest(
command=["python", "/app/manage.py", "migrate", "--database=production"]
)
migration_response = client.containers.execute_command(
resource_group_name=resource_group,
container_group_name=container_group,
container_name=container_name,
container_exec_request=migration_request
)
print(f"Migration started. WebSocket URI: {migration_response.web_socket_uri}")
# Wait and check logs for migration results
time.sleep(30)
migration_logs = client.containers.list_logs(
resource_group_name=resource_group,
container_group_name=container_group,
container_name=container_name,
tail=20
)
if "Migration completed successfully" in migration_logs.content:
print("Database migration completed successfully")
else:
print("Migration may have failed. Check logs:")
print(migration_logs.content)
# Usage
run_database_migration(
client=client,
resource_group="production-rg",
container_group="backend-cg",
container_name="api-server"
)def start_debug_session(client, resource_group, container_group, container_name):
"""Start an interactive debugging session in a container."""
from azure.mgmt.containerinstance.models import ContainerExecRequestTerminalSize
# Start interactive bash session
debug_request = ContainerExecRequest(
command=["/bin/bash"],
terminal_size=ContainerExecRequestTerminalSize(rows=30, cols=120)
)
debug_response = client.containers.execute_command(
resource_group_name=resource_group,
container_group_name=container_group,
container_name=container_name,
container_exec_request=debug_request
)
print("Debug session started!")
print(f"WebSocket URI: {debug_response.web_socket_uri}")
print(f"Password: {debug_response.password}")
print("Use a WebSocket client to connect and start debugging")
return debug_response
# Usage
debug_session = start_debug_session(
client=client,
resource_group="development-rg",
container_group="test-app-cg",
container_name="debug-container"
)The execute_command and attach operations return WebSocket connection details that require additional handling:
import websocket
import json
def connect_to_container_websocket(websocket_uri, password):
"""Example WebSocket connection handler for container operations."""
def on_message(ws, message):
print(f"Container output: {message}")
def on_error(ws, error):
print(f"WebSocket error: {error}")
def on_close(ws, close_status_code, close_msg):
print("WebSocket connection closed")
def on_open(ws):
print("WebSocket connection established")
# Send authentication if required
auth_message = {"password": password}
ws.send(json.dumps(auth_message))
# Create WebSocket connection
ws = websocket.WebSocketApp(
websocket_uri,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
# Start connection
ws.run_forever()
# Example usage with command execution
exec_response = client.containers.execute_command(
resource_group_name="my-rg",
container_group_name="my-cg",
container_name="my-container",
container_exec_request=ContainerExecRequest(command=["echo", "Hello World"])
)
# Connect to WebSocket to see command output
connect_to_container_websocket(
exec_response.web_socket_uri,
exec_response.password
)Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-containerinstance