0
# Container Group Operations
1
2
Container Groups are the primary deployment unit in Azure Container Instances, representing a collection of containers that share the same lifecycle, network, and storage resources.
3
4
## Core Operations
5
6
### List Container Groups { .api }
7
8
```python
9
def list(**kwargs) -> ItemPaged[ContainerGroup]:
10
"""
11
List all container groups in the subscription.
12
13
Returns:
14
ItemPaged[ContainerGroup]: Paginated list of container groups across all resource groups
15
16
Example:
17
container_groups = client.container_groups.list()
18
for group in container_groups:
19
print(f"Container Group: {group.name} in {group.location}")
20
"""
21
22
def list_by_resource_group(resource_group_name: str, **kwargs) -> ItemPaged[ContainerGroup]:
23
"""
24
List container groups in a specific resource group.
25
26
Args:
27
resource_group_name (str): Name of the resource group
28
29
Returns:
30
ItemPaged[ContainerGroup]: Paginated list of container groups in the resource group
31
32
Example:
33
container_groups = client.container_groups.list_by_resource_group("my-resource-group")
34
for group in container_groups:
35
print(f"Container Group: {group.name}, Status: {group.provisioning_state}")
36
"""
37
```
38
39
### Get Container Group { .api }
40
41
```python
42
def get(resource_group_name: str, container_group_name: str, **kwargs) -> ContainerGroup:
43
"""
44
Get details of a specific container group.
45
46
Args:
47
resource_group_name (str): Name of the resource group
48
container_group_name (str): Name of the container group
49
50
Returns:
51
ContainerGroup: Complete container group configuration and status
52
53
Raises:
54
ResourceNotFoundError: If the container group doesn't exist
55
56
Example:
57
container_group = client.container_groups.get("my-resource-group", "my-container-group")
58
print(f"IP Address: {container_group.ip_address.ip}")
59
print(f"State: {container_group.provisioning_state}")
60
"""
61
```
62
63
### Create or Update Container Group { .api }
64
65
```python
66
def begin_create_or_update(
67
resource_group_name: str,
68
container_group_name: str,
69
container_group: ContainerGroup,
70
**kwargs
71
) -> LROPoller[ContainerGroup]:
72
"""
73
Create a new container group or update an existing one.
74
75
Args:
76
resource_group_name (str): Name of the resource group
77
container_group_name (str): Name for the container group
78
container_group (ContainerGroup): Container group configuration
79
80
Returns:
81
LROPoller[ContainerGroup]: Long-running operation poller for the deployment
82
83
Example:
84
from azure.mgmt.containerinstance.models import (
85
ContainerGroup, Container, ContainerGroupProperties,
86
ResourceRequirements, ResourceRequests, IpAddress, ContainerPort
87
)
88
89
# Define container
90
container = Container(
91
name="web-server",
92
image="nginx:1.21",
93
resources=ResourceRequirements(
94
requests=ResourceRequests(memory_in_gb=1.0, cpu=1.0)
95
),
96
ports=[ContainerPort(port=80)]
97
)
98
99
# Define container group
100
container_group = ContainerGroup(
101
location="East US",
102
containers=[container],
103
os_type="Linux",
104
restart_policy="Always",
105
ip_address=IpAddress(
106
type="Public",
107
ports=[ContainerPort(port=80, protocol="TCP")]
108
)
109
)
110
111
# Create container group
112
operation = client.container_groups.begin_create_or_update(
113
resource_group_name="my-resource-group",
114
container_group_name="my-web-app",
115
container_group=container_group
116
)
117
118
# Wait for completion
119
result = operation.result()
120
print(f"Container group created with IP: {result.ip_address.ip}")
121
"""
122
```
123
124
### Update Container Group Tags { .api }
125
126
```python
127
def update(
128
resource_group_name: str,
129
container_group_name: str,
130
resource: Resource,
131
**kwargs
132
) -> ContainerGroup:
133
"""
134
Update container group tags without modifying the container configuration.
135
136
Args:
137
resource_group_name (str): Name of the resource group
138
container_group_name (str): Name of the container group
139
resource (Resource): Resource object containing updated tags
140
141
Returns:
142
ContainerGroup: Updated container group
143
144
Example:
145
from azure.mgmt.containerinstance.models import Resource
146
147
# Update tags
148
resource_update = Resource(tags={"environment": "production", "team": "backend"})
149
updated_group = client.container_groups.update(
150
resource_group_name="my-resource-group",
151
container_group_name="my-container-group",
152
resource=resource_update
153
)
154
print(f"Updated tags: {updated_group.tags}")
155
"""
156
```
157
158
### Delete Container Group { .api }
159
160
```python
161
def begin_delete(
162
resource_group_name: str,
163
container_group_name: str,
164
**kwargs
165
) -> LROPoller[ContainerGroup]:
166
"""
167
Delete a container group and all its containers.
168
169
Args:
170
resource_group_name (str): Name of the resource group
171
container_group_name (str): Name of the container group to delete
172
173
Returns:
174
LROPoller[ContainerGroup]: Long-running operation poller for the deletion
175
176
Example:
177
# Delete container group
178
delete_operation = client.container_groups.begin_delete(
179
resource_group_name="my-resource-group",
180
container_group_name="my-container-group"
181
)
182
183
# Wait for deletion to complete
184
delete_operation.result()
185
print("Container group deleted successfully")
186
"""
187
```
188
189
## Lifecycle Operations
190
191
### Restart Container Group { .api }
192
193
```python
194
def begin_restart(
195
resource_group_name: str,
196
container_group_name: str,
197
**kwargs
198
) -> LROPoller[None]:
199
"""
200
Restart all containers in a container group.
201
202
Args:
203
resource_group_name (str): Name of the resource group
204
container_group_name (str): Name of the container group
205
206
Returns:
207
LROPoller[None]: Long-running operation poller for the restart
208
209
Example:
210
# Restart container group
211
restart_operation = client.container_groups.begin_restart(
212
resource_group_name="my-resource-group",
213
container_group_name="my-container-group"
214
)
215
216
# Wait for restart to complete
217
restart_operation.result()
218
print("Container group restarted successfully")
219
"""
220
```
221
222
### Stop Container Group { .api }
223
224
```python
225
def stop(resource_group_name: str, container_group_name: str, **kwargs) -> None:
226
"""
227
Stop all containers in a container group.
228
229
Args:
230
resource_group_name (str): Name of the resource group
231
container_group_name (str): Name of the container group
232
233
Example:
234
# Stop container group
235
client.container_groups.stop(
236
resource_group_name="my-resource-group",
237
container_group_name="my-container-group"
238
)
239
print("Container group stopped")
240
"""
241
```
242
243
### Start Container Group { .api }
244
245
```python
246
def begin_start(
247
resource_group_name: str,
248
container_group_name: str,
249
**kwargs
250
) -> LROPoller[None]:
251
"""
252
Start all containers in a stopped container group.
253
254
Args:
255
resource_group_name (str): Name of the resource group
256
container_group_name (str): Name of the container group
257
258
Returns:
259
LROPoller[None]: Long-running operation poller for the start operation
260
261
Example:
262
# Start container group
263
start_operation = client.container_groups.begin_start(
264
resource_group_name="my-resource-group",
265
container_group_name="my-container-group"
266
)
267
268
# Wait for start to complete
269
start_operation.result()
270
print("Container group started successfully")
271
"""
272
```
273
274
## Common Usage Patterns
275
276
### Multi-Container Application
277
278
```python
279
from azure.mgmt.containerinstance.models import (
280
ContainerGroup, Container, ContainerGroupProperties,
281
ResourceRequirements, ResourceRequests, IpAddress, ContainerPort,
282
EnvironmentVariable, Volume, VolumeMount, AzureFileVolume
283
)
284
285
# Define frontend container
286
frontend = Container(
287
name="frontend",
288
image="myapp/frontend:latest",
289
resources=ResourceRequirements(
290
requests=ResourceRequests(memory_in_gb=0.5, cpu=0.5)
291
),
292
ports=[ContainerPort(port=80)],
293
environment_variables=[
294
EnvironmentVariable(name="API_URL", value="http://localhost:3000")
295
]
296
)
297
298
# Define backend container
299
backend = Container(
300
name="backend",
301
image="myapp/backend:latest",
302
resources=ResourceRequirements(
303
requests=ResourceRequests(memory_in_gb=1.0, cpu=1.0)
304
),
305
ports=[ContainerPort(port=3000)],
306
environment_variables=[
307
EnvironmentVariable(name="DATABASE_URL", secure_value="connection-string")
308
]
309
)
310
311
# Create container group with multiple containers
312
container_group = ContainerGroup(
313
location="East US",
314
containers=[frontend, backend],
315
os_type="Linux",
316
restart_policy="Always",
317
ip_address=IpAddress(
318
type="Public",
319
ports=[
320
ContainerPort(port=80, protocol="TCP"),
321
ContainerPort(port=3000, protocol="TCP")
322
]
323
)
324
)
325
326
# Deploy the multi-container application
327
operation = client.container_groups.begin_create_or_update(
328
resource_group_name="my-resource-group",
329
container_group_name="my-multi-container-app",
330
container_group=container_group
331
)
332
result = operation.result()
333
```
334
335
### Container Group with Persistent Storage
336
337
```python
338
# Define Azure File share volume
339
azure_file_volume = Volume(
340
name="shared-storage",
341
azure_file=AzureFileVolume(
342
share_name="my-file-share",
343
storage_account_name="mystorageaccount",
344
storage_account_key="storage-account-key"
345
)
346
)
347
348
# Container with mounted volume
349
container = Container(
350
name="data-processor",
351
image="ubuntu:20.04",
352
command=["/bin/bash", "-c", "while true; do echo 'Processing data...'; sleep 30; done"],
353
resources=ResourceRequirements(
354
requests=ResourceRequests(memory_in_gb=1.0, cpu=1.0)
355
),
356
volume_mounts=[
357
VolumeMount(
358
name="shared-storage",
359
mount_path="/data",
360
read_only=False
361
)
362
]
363
)
364
365
# Container group with persistent storage
366
container_group = ContainerGroup(
367
location="East US",
368
containers=[container],
369
os_type="Linux",
370
volumes=[azure_file_volume],
371
restart_policy="OnFailure"
372
)
373
```
374
375
## Network Dependencies
376
377
### Get Outbound Network Dependencies { .api }
378
379
```python
380
def get_outbound_network_dependencies_endpoints(
381
resource_group_name: str,
382
container_group_name: str,
383
**kwargs
384
) -> List[str]:
385
"""
386
Get outbound network dependencies for a container group.
387
388
Args:
389
resource_group_name (str): Name of the resource group
390
container_group_name (str): Name of the container group
391
392
Returns:
393
List[str]: List of outbound network dependency endpoints
394
395
Example:
396
# Get network dependencies for troubleshooting
397
dependencies = client.container_groups.get_outbound_network_dependencies_endpoints(
398
resource_group_name="my-resource-group",
399
container_group_name="my-container-group"
400
)
401
402
print("Outbound network dependencies:")
403
for endpoint in dependencies:
404
print(f" - {endpoint}")
405
"""
406
```
407
408
### Network Troubleshooting Example
409
410
```python
411
def diagnose_container_group_networking(client, resource_group, container_group_name):
412
"""Comprehensive network diagnostics for a container group."""
413
414
print(f"Diagnosing network configuration for: {container_group_name}")
415
416
try:
417
# Get container group details
418
container_group = client.container_groups.get(
419
resource_group_name=resource_group,
420
container_group_name=container_group_name
421
)
422
423
print(f"Container Group Status: {container_group.provisioning_state}")
424
425
# Check IP configuration
426
if container_group.ip_address:
427
print(f"IP Address: {container_group.ip_address.ip}")
428
print(f"IP Type: {container_group.ip_address.type}")
429
if container_group.ip_address.ports:
430
print("Exposed Ports:")
431
for port in container_group.ip_address.ports:
432
print(f" - {port.port}/{port.protocol}")
433
else:
434
print("No public IP address configured")
435
436
# Check virtual network integration
437
if container_group.subnet_ids:
438
print("Virtual Network Integration:")
439
for subnet in container_group.subnet_ids:
440
print(f" - Subnet: {subnet.id}")
441
442
# Get network dependencies
443
try:
444
dependencies = client.container_groups.get_outbound_network_dependencies_endpoints(
445
resource_group_name=resource_group,
446
container_group_name=container_group_name
447
)
448
449
if dependencies:
450
print("Outbound Network Dependencies:")
451
for endpoint in dependencies:
452
print(f" - {endpoint}")
453
else:
454
print("No outbound network dependencies found")
455
456
except Exception as e:
457
print(f"Failed to retrieve network dependencies: {e}")
458
459
except Exception as e:
460
print(f"Failed to retrieve container group details: {e}")
461
462
# Usage
463
diagnose_container_group_networking(
464
client=client,
465
resource_group="production-rg",
466
container_group_name="api-container-group"
467
)
468
```