A Python library for the Docker Engine API.
—
Docker Swarm service orchestration including service creation, scaling, updating, and management with support for service constraints, placement preferences, and rolling updates.
class ServiceCollection:
def create(self, image, **kwargs):
"""
Create a Docker service.
Args:
image (str): Image name for service
**kwargs: Service configuration options
Returns:
Service: Created service instance
Common kwargs:
command (list): Command to execute
args (list): Command arguments
constraints (list): Placement constraints
preferences (list): Placement preferences
platforms (list): Platform constraints
name (str): Service name
labels (dict): Service labels
mode (dict): Service mode (replicated/global)
update_config (dict): Update configuration
rollback_config (dict): Rollback configuration
networks (list): Networks to attach
endpoint_spec (dict): Endpoint configuration
resources (dict): Resource requirements
restart_policy (dict): Restart policy
log_driver (dict): Logging configuration
"""
def get(self, service_id):
"""
Get a service by ID or name.
Args:
service_id (str): Service ID or name
Returns:
Service: Service instance
"""
def list(self, filters=None):
"""
List services.
Args:
filters (dict): Filter results by id, name, label, mode
Returns:
list[Service]: List of service instances
"""class Service:
"""
A Docker service instance.
Properties:
id (str): Service ID
name (str): Service name
attrs (dict): Raw service attributes
"""
def logs(self, **kwargs):
"""
Get service logs.
Args:
**kwargs: Log options (details, follow, stdout, stderr, since, timestamps, tail)
Returns:
generator: Service log entries
"""
def remove(self):
"""Remove the service."""
def scale(self, replicas):
"""
Scale the service.
Args:
replicas (int): Target number of replicas
"""
def update(self, **kwargs):
"""
Update service configuration.
Args:
**kwargs: Service update options (image, command, args, etc.)
"""import docker
client = docker.from_env()
# Create service with constraints
service = client.services.create(
image='nginx:latest',
name='web-service',
mode={'Replicated': {'Replicas': 3}},
constraints=['node.role == worker'],
networks=['my-overlay-network'],
endpoint_spec={'Ports': [{'TargetPort': 80, 'PublishedPort': 8080}]}
)
# Scale service
service.scale(5)
# Update service image
service.update(image='nginx:1.21')