0
# Task Management
1
2
Docker swarm task inspection and monitoring. Tasks represent individual running instances of services on specific nodes in the swarm cluster, providing detailed execution and status information.
3
4
## Capabilities
5
6
### Task Listing
7
8
List all tasks in the swarm cluster.
9
10
```python { .api }
11
def list() -> List[Task]:
12
"""
13
List all tasks in the swarm.
14
15
Returns:
16
List of Task objects with status and assignment information
17
"""
18
```
19
20
### Task Inspection
21
22
Inspect detailed task information including specifications and status.
23
24
```python { .api }
25
def inspect(x: Union[str, List[str]]) -> Union[Task, List[Task]]:
26
"""
27
Inspect one or more tasks.
28
29
Parameters:
30
- x: Task ID(s) - tasks are identified by their unique IDs
31
32
Returns:
33
Task object(s) with detailed information
34
"""
35
```
36
37
**Usage Examples:**
38
39
```python
40
from python_on_whales import docker
41
42
# List all tasks in the swarm
43
tasks = docker.task.list()
44
for task in tasks:
45
print(f"Task: {task.name}")
46
print(f" Service: {task.service_id}")
47
print(f" Node: {task.node_id}")
48
print(f" State: {task.desired_state}")
49
print(f" Status: {task.status.state}")
50
print(f" Slot: {task.slot}")
51
52
# Inspect specific task
53
task_id = "ktnakcyq2qg0kpgvjkb"
54
task = docker.task.inspect(task_id)
55
print(f"Task Details:")
56
print(f" Created: {task.created_at}")
57
print(f" Updated: {task.updated_at}")
58
print(f" Image: {task.spec.container_spec.image}")
59
print(f" Resources: {task.spec.resources}")
60
61
# Filter tasks by service (using service.ps() method)
62
web_service_tasks = docker.service.ps("web-service")
63
for task in web_service_tasks:
64
print(f"Web Service Task: {task.name} - Status: {task.status.state}")
65
66
# Filter tasks by node (using node.ps() method)
67
node_tasks = docker.node.ps("worker-node-01")
68
for task in node_tasks:
69
print(f"Node Task: {task.name} - Service: {task.service_id}")
70
```
71
72
## Types
73
74
```python { .api }
75
class Task:
76
id: str
77
version: DockerObjectVersion
78
created_at: datetime
79
updated_at: datetime
80
name: str
81
labels: Dict[str, str]
82
spec: TaskSpec
83
service_id: str
84
slot: Optional[int]
85
node_id: str
86
assigned_generic_resources: List[Dict[str, Any]]
87
status: TaskStatus
88
desired_state: str
89
90
class TaskSpec:
91
container_spec: ContainerSpec
92
resources: Optional[ResourceRequirements]
93
restart_policy: Optional[RestartPolicy]
94
placement: Optional[Placement]
95
log_driver: Optional[LogDriver]
96
networks: List[NetworkAttachmentConfig]
97
force_update: int
98
99
class TaskStatus:
100
timestamp: datetime
101
state: str # "new", "assigned", "accepted", "preparing", "ready", "starting", "running", "complete", "shutdown", "failed", "rejected", "remove", "orphaned"
102
message: Optional[str]
103
err: Optional[str]
104
container_status: Optional[ContainerStatus]
105
port_status: Optional[PortStatus]
106
107
class ContainerStatus:
108
container_id: str
109
pid: int
110
exit_code: Optional[int]
111
112
class ContainerSpec:
113
image: str
114
labels: Dict[str, str]
115
command: Optional[List[str]]
116
args: Optional[List[str]]
117
hostname: Optional[str]
118
env: Optional[List[str]]
119
dir: Optional[str]
120
user: Optional[str]
121
groups: Optional[List[str]]
122
privileges: Optional[Dict[str, Any]]
123
tty: bool
124
open_stdin: bool
125
read_only: bool
126
mounts: List[Mount]
127
stop_signal: Optional[str]
128
stop_grace_period: Optional[int]
129
health_check: Optional[HealthConfig]
130
hosts: Optional[List[str]]
131
dns_config: Optional[DNSConfig]
132
secrets: Optional[List[SecretReference]]
133
configs: Optional[List[ConfigReference]]
134
ulimits: Optional[List[Ulimit]]
135
sysctls: Optional[Dict[str, str]]
136
137
class ResourceRequirements:
138
limits: Optional[Resources]
139
reservations: Optional[Resources]
140
141
class Resources:
142
nano_cpus: Optional[int]
143
memory_bytes: Optional[int]
144
generic_resources: Optional[List[GenericResource]]
145
146
class RestartPolicy:
147
condition: str # "none", "on-failure", "any"
148
delay: Optional[int]
149
max_attempts: Optional[int]
150
window: Optional[int]
151
152
class Placement:
153
constraints: Optional[List[str]]
154
preferences: Optional[List[PlacementPreference]]
155
max_replicas: Optional[int]
156
platforms: Optional[List[Platform]]
157
158
class LogDriver:
159
name: str
160
options: Optional[Dict[str, str]]
161
162
class DockerObjectVersion:
163
index: int
164
```