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
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 ...
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.
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.
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.
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] ~
$
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With