How to stop a docker service?

In Docker, the concept of a "service" signifies a resource that represents an abstraction of a bunch of containers. It can exist or not exist, but you can't "run" a service. You can only run containers. Therefore you can't "stop" a service either, you can only remove it.


The service defines a target state. So rather than stopping and starting the service, if you want to stop the deployed containers without deleting the service, you would change the target state to be one where no containers would run. For a replicated service, setting the replica count to 0 works well, and for a global service, adding a constraint that is not matched on any node would work:

docker service update --replicas 0 $service_name
docker service update --constraint-add no_such_node=true $service_name

To delete the service, which stops all deployed containers, you run:

docker service rm $service_name

docker service scale [servicename]=0

will remove all running instances but still keep the service object alive.

eg.

[node1] (local) [email protected] ~
$ docker service create --name test-service nginx
cjvbwuhjhwpixwg01nh22cqbq
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
[node1] (local) [email protected] ~
$ docker service scale test-service=0
test-service scaled to 0
overall progress: 0 out of 0 tasks
verify: Service converged
[node1] (local) [email protected] ~
$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
cjvbwuhjhwpi        test-service        replicated          0/0                 nginx:latest
[node1] (local) [email protected] ~
$

Tags:

Docker