3

Let's say I have the Docker Swarm services myservice1 and myservice2. How can I remove these services and wait for the associated containers to stop?

redgeoff
  • 201
  • 1
  • 6

2 Answers2

6

To stop and wait for a single service:

$ docker service rm myservice1 && docker wait $(docker ps -f "name=myservice1" -q)

For multiple services:

$ docker service rm myservice1 myservice2 && docker wait $(docker ps -f "name=myservice1" -f "name=myservice2" -q)
redgeoff
  • 201
  • 1
  • 6
  • 1
    this is only working, if the containers are on the docker-host where the command is issued. should one have multiple docker-hosts in a swarm (what swarm is intended for) this is not working anymore – smoebody Dec 04 '19 at 13:38
0

Unfortunately, I need more rep to comment on /u/redgeoff's answer...but the docker wait operates on containers/tasks which are local to the node the command is executed on. In a multi-node swarm (sort of the point of using docker swarm), the node you're on isn't guaranteed to be where the service task is running.

Something like this should be better:

$ docker service ps --format '{{.Node}}' serviceName
# node1.swarm.internal
$ ssh user@node1.swarm.internal
$ docker service rm myservice1 && docker wait $(docker ps -f "name=serviceName" -q)

However, this also assumes the node the task is running on is a manager node. If it's not, then docker service commands cannot run. In that case, you should run the docker service rm command before ssh'ing into where the task exists.

404
  • 101