Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a docker service?

Tags:

docker

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
like image 899
DenCowboy Avatar asked Jun 29 '18 13:06

DenCowboy


People also ask

How to stop a Docker application container?

There are two steps to stop a docker application containers: 1 First, stop the running containers using docker-compose stop 2 Second, remove the stopped containers using docker-compose rm -f More ...

What is a service in Docker?

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.

How to restart multiple Docker containers at once?

To summarize, if you just want to restart multiple containers that are created by docker-compose.yml file, use the following commands in sequence. This will first stop all the containers, next remove all the containers, and finally start them in the background as specified by the docker-compose.yml file.

How to list all the running Docker containers in Linux?

You can list all the running docker containers with the docker ps command. Without any options, the docker ps command only shows the running containers.


3 Answers

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] ~
$
like image 168
person one Avatar answered Oct 24 '22 15:10

person one


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.

like image 26
anothernode Avatar answered Oct 24 '22 14:10

anothernode


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
like image 26
BMitch Avatar answered Oct 24 '22 14:10

BMitch