28

How can I stop a docker service without removing (rm) it?

Commands:
  create      Create a new service
  inspect     Display detailed information on one or more services
  logs        Fetch the logs of a service or task
  ls          List services
  ps          List the tasks of one or more services
  rm          Remove one or more services
  rollback    Revert changes to a service's configuration
  scale       Scale one or multiple replicated services
  update      Update a service
DenCowboy
  • 13,884
  • 38
  • 114
  • 210
  • Docker service or docker container? Docker container is just a process, find it and kill it. – Mike Doe Jun 29 '18 at 13:14
  • docker services like in a swarm cluster: https://docs.docker.com/engine/swarm/services/#publish-a-services-ports-using-the-routing-mesh – DenCowboy Jun 29 '18 at 13:15
  • Related: [How to stop a deployed docker stack](https://stackoverflow.com/q/41838150/596285) – BMitch Jul 23 '19 at 15:49

3 Answers3

39
docker service scale [servicename]=0

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

eg.

[node1] (local) root@192.168.0.8 ~
$ docker service create --name test-service nginx
cjvbwuhjhwpixwg01nh22cqbq
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
[node1] (local) root@192.168.0.8 ~
$ docker service scale test-service=0
test-service scaled to 0
overall progress: 0 out of 0 tasks
verify: Service converged
[node1] (local) root@192.168.0.8 ~
$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
cjvbwuhjhwpi        test-service        replicated          0/0                 nginx:latest
[node1] (local) root@192.168.0.8 ~
$
person one
  • 501
  • 1
  • 5
  • 6
  • 1
    This will scale containers from service if service is configured in replication mode, but if service is configured in global mode, it will through error. – vinay Jul 18 '19 at 11:10
20

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.

anothernode
  • 5,100
  • 13
  • 43
  • 62
6

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
BMitch
  • 231,797
  • 42
  • 475
  • 450