蜂群服务

警告:这是一个过时的文档,可能包含过时的信息。更新的类和方法签名,请参考API文档。

从引擎1.12(API 1.24)开始,可以使用Docker Engine API管理服务。请注意,引擎需要是 [蜂群集群] (../swarm.html),然后才能使用与服务相关的方法。

创建服务

这个 APIClient.create_service 方法允许您在群集内创建新服务。该方法接受几个参数, task_template 是强制性的。该值字典最容易生成的方法是实例化 TaskTemplate 对象。

container_spec = docker.types.ContainerSpec(
    image='busybox', command=['echo', 'hello']
)
task_tmpl = docker.types.TaskTemplate(container_spec)
service_id = client.create_service(task_tmpl, name=name)

上市服务

列出所有现有服务,使用 APIClient.services 方法。

client.services(filters={'name': 'mysql'})

正在检索服务配置

要检索特定服务的详细信息和配置,可以使用 APIClient.inspect_service 方法使用服务的ID或名称。

client.inspect_service(service='my_service_name')

正在更新服务配置

这个 APIClient.update_service 方法允许您更新服务的配置。强制性的 version 参数(用于防止并发写入)可以使用 APIClient.inspect_service

container_spec = docker.types.ContainerSpec(
    image='busybox', command=['echo', 'hello world']
)
task_tmpl = docker.types.TaskTemplate(container_spec)

svc_version = client.inspect_service(svc_id)['Version']['Index']

client.update_service(
    svc_id, svc_version, name='new_name', task_template=task_tmpl
)

删除服务

只需使用 APIClient.remove_service 方法。服务名称或服务ID都可以用作参数。

client.remove_service('my_service_name')