Models

Docker Swarm Services

This section outlines the models used to represent Docker Swarm services and their configurations.

docker.models.services.Service represents a Docker Swarm service. It provides methods for interacting with a service, such as starting, stopping, scaling, and updating.

from docker import from_env
          from docker.models.services import Service
          
          client = from_env()
          
          # Create a new service
          service = client.services.create(
              'nginx:latest',
              name='my-nginx',
              ports=[('80', '8080')],
              networks=['my-network'],
              restart_policy={'condition': 'on-failure'},
          )
          
          # Get the service by its name
          service = client.services.get('my-nginx')
          
          # Update the service
          service.update(
              image='nginx:1.14',
              restart_policy={'condition': 'any'},
          )
          
          # Stop the service
          service.remove()
          
          # Start the service
          service.start()
          
          # Stop the service
          service.stop()
          
          # Scale the service to 3 replicas
          service.scale(3)
          
          # Get the logs of the service
          logs = service.logs()
          

docker.models.services.Task represents a Docker Swarm task. It provides methods for interacting with a task, such as retrieving its status and logs.

from docker import from_env
          from docker.models.services import Task
          
          client = from_env()
          
          # Get the task by its ID
          task = client.tasks.get('my-task-id')
          
          # Get the task's status
          task.status
          
          # Get the task's logs
          logs = task.logs()
          

docker.models.services.Config represents a Docker Swarm configuration. It provides methods for interacting with a configuration, such as creating, updating, and removing it.

from docker import from_env
          from docker.models.services.config import Config
          
          client = from_env()
          
          # Create a new configuration
          config = client.configs.create(
              'my-config',
              labels={'foo': 'bar'},
              data='some data',
              template='template.yaml',
          )
          
          # Get the configuration by its name
          config = client.configs.get('my-config')
          
          # Update the configuration
          config.update(
              labels={'baz': 'qux'},
          )
          
          # Remove the configuration
          config.remove()
          

docker.models.services.Secret represents a Docker Swarm secret. It provides methods for interacting with a secret, such as creating, updating, and removing it.

from docker import from_env
          from docker.models.services.secret import Secret
          
          client = from_env()
          
          # Create a new secret
          secret = client.secrets.create(
              'my-secret',
              labels={'foo': 'bar'},
              data='some data',
          )
          
          # Get the secret by its name
          secret = client.secrets.get('my-secret')
          
          # Update the secret
          secret.update(
              labels={'baz': 'qux'},
          )
          
          # Remove the secret
          secret.remove()
          

docker.models.services.Network represents a Docker Swarm network. It provides methods for interacting with a network, such as creating, updating, and removing it.

from docker import from_env
          from docker.models.services.network import Network
          
          client = from_env()
          
          # Create a new network
          network = client.networks.create(
              'my-network',
              driver='bridge',
              labels={'foo': 'bar'},
          )
          
          # Get the network by its name
          network = client.networks.get('my-network')
          
          # Update the network
          network.update(
              labels={'baz': 'qux'},
          )
          
          # Remove the network
          network.remove()
          

docker.models.services.ServiceSpec represents the specification of a Docker Swarm service. It contains information about the service’s image, ports, networks, restart policy, and other settings.

from docker.models.services import ServiceSpec
          
          # Create a new service spec
          spec = ServiceSpec(
              image='nginx:latest',
              ports=[('80', '8080')],
              networks=['my-network'],
              restart_policy={'condition': 'on-failure'},
          )
          
          # Get the service spec from a service object
          service = client.services.get('my-nginx')
          spec = service.attrs['Spec']
          

docker.models.services.TaskTemplate represents the task template for a Docker Swarm service. It contains information about the task’s resources, placement constraints, and other settings.

from docker.models.services import TaskTemplate
          
          # Create a new task template
          template = TaskTemplate(
              resources={'limits': {'memory': '512M'}},
              placement={'constraints': ['node.role==manager']},
          )
          
          # Get the task template from a service object
          service = client.services.get('my-nginx')
          template = service.attrs['Spec']['TaskTemplate']
          

docker.models.services.Mount represents a mount point for a Docker Swarm service. It contains information about the mount’s source, target, and type.

from docker.models.services import Mount
          
          # Create a new mount
          mount = Mount(
              target='/data',
              source='/var/lib/data',
              type='bind',
          )
          
          # Get the mounts from a service object
          service = client.services.get('my-nginx')
          mounts = service.attrs['Spec']['TaskTemplate']['ContainerSpec']['Mounts']
          

docker.models.services.EndpointSpec represents the endpoint specification for a Docker Swarm service. It contains information about the service’s ports and networks.

from docker.models.services import EndpointSpec
          
          # Create a new endpoint spec
          spec = EndpointSpec(
              ports=[('80', '8080')],
              networks=['my-network'],
          )
          
          # Get the endpoint spec from a service object
          service = client.services.get('my-nginx')
          spec = service.attrs['Spec']['EndpointSpec']
          

docker.models.services.UpdateConfig represents the update configuration for a Docker Swarm service. It contains information about the update’s parallelism, delay, and other settings.

from docker.models.services import UpdateConfig
          
          # Create a new update config
          config = UpdateConfig(
              parallelism=2,
              delay=10,
              failure_action='pause',
              monitor=120,
              max_failure_ratio=0.1,
          )
          
          # Get the update config from a service object
          service = client.services.get('my-nginx')
          config = service.attrs['UpdateConfig']