0
# Docker Swarm Services
1
2
Docker Swarm service orchestration including service creation, scaling, updating, and management with support for service constraints, placement preferences, and rolling updates.
3
4
## Capabilities
5
6
### ServiceCollection
7
8
```python { .api }
9
class ServiceCollection:
10
def create(self, image, **kwargs):
11
"""
12
Create a Docker service.
13
14
Args:
15
image (str): Image name for service
16
**kwargs: Service configuration options
17
18
Returns:
19
Service: Created service instance
20
21
Common kwargs:
22
command (list): Command to execute
23
args (list): Command arguments
24
constraints (list): Placement constraints
25
preferences (list): Placement preferences
26
platforms (list): Platform constraints
27
name (str): Service name
28
labels (dict): Service labels
29
mode (dict): Service mode (replicated/global)
30
update_config (dict): Update configuration
31
rollback_config (dict): Rollback configuration
32
networks (list): Networks to attach
33
endpoint_spec (dict): Endpoint configuration
34
resources (dict): Resource requirements
35
restart_policy (dict): Restart policy
36
log_driver (dict): Logging configuration
37
"""
38
39
def get(self, service_id):
40
"""
41
Get a service by ID or name.
42
43
Args:
44
service_id (str): Service ID or name
45
46
Returns:
47
Service: Service instance
48
"""
49
50
def list(self, filters=None):
51
"""
52
List services.
53
54
Args:
55
filters (dict): Filter results by id, name, label, mode
56
57
Returns:
58
list[Service]: List of service instances
59
"""
60
```
61
62
### Service Model
63
64
```python { .api }
65
class Service:
66
"""
67
A Docker service instance.
68
69
Properties:
70
id (str): Service ID
71
name (str): Service name
72
attrs (dict): Raw service attributes
73
"""
74
75
def logs(self, **kwargs):
76
"""
77
Get service logs.
78
79
Args:
80
**kwargs: Log options (details, follow, stdout, stderr, since, timestamps, tail)
81
82
Returns:
83
generator: Service log entries
84
"""
85
86
def remove(self):
87
"""Remove the service."""
88
89
def scale(self, replicas):
90
"""
91
Scale the service.
92
93
Args:
94
replicas (int): Target number of replicas
95
"""
96
97
def update(self, **kwargs):
98
"""
99
Update service configuration.
100
101
Args:
102
**kwargs: Service update options (image, command, args, etc.)
103
"""
104
```
105
106
## Usage Examples
107
108
```python
109
import docker
110
111
client = docker.from_env()
112
113
# Create service with constraints
114
service = client.services.create(
115
image='nginx:latest',
116
name='web-service',
117
mode={'Replicated': {'Replicas': 3}},
118
constraints=['node.role == worker'],
119
networks=['my-overlay-network'],
120
endpoint_spec={'Ports': [{'TargetPort': 80, 'PublishedPort': 8080}]}
121
)
122
123
# Scale service
124
service.scale(5)
125
126
# Update service image
127
service.update(image='nginx:1.21')
128
```