Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart multiple containers in a docker swarm

I currently have 8 containers across 4 different host machines in my docker setup.

ubuntu@ip-192-168-1-8:~$ docker service ls
ID            NAME         MODE    REPLICAS  IMAGE
yyyytxxxxx8  mycontainers  global  8/8     myapplications:latest

Running a ps -a on the service yields the following.

ubuntu@ip-192-168-1-8:~$ docker service ps -a yy
NAME                            IMAGE                        NODE          DESIRED STATE  CURRENT STATE        ERROR  PORTS
 mycontainers1                myapplications:latest:   ip-192-168-1-5 Running        Running 3 hours ago
 \_ mycontainers2                myapplications:latest: ip-192-168-1-6  Running        Running 3 hours ago
 \_ mycontainers3                myapplications:latest:  ip-192-168-1-7  Running        Running 3 hours ago
\_ mycontainers4                myapplications:latest:  ip-192-168-1-8  Running        Running 3 hours ago
\_ mycontainers1                myapplications:latest: ip-192-168-1-5  Running        Running 3 hours ago
 \_ mycontainers2                myapplications:latest:  ip-192-168-1-6  Running        Running 3 hours ago
\_ mycontainers3                myapplications:latest:  ip-192-168-1-7  Running        Running 3 hours ago
\_ mycontainers4                myapplications:latest:  ip-192-168-1-8  Running        Running 3 hours ago

My question is how can i execute a restart of all the containers using the service ID? I dont want to manually log into every node and execute a restart.

like image 620
crusadecoder Avatar asked Dec 29 '16 22:12

crusadecoder


3 Answers

In the latest stable version of docker 1.12.x, it is possible to restart the container by updating the service configuration, but in the docker 1.13.0 which is released soon, even if the service setting is not changed, by specifying the --force flag, the container will be restarted. If you do not mind to use the 1.13.0 RC4 you can do it now.

$ docker service update --force mycontainers

Update: Docker 1.13.0 has been released.

https://github.com/docker/docker/releases/tag/v1.13.0

like image 172
minamijoyo Avatar answered Nov 08 '22 21:11

minamijoyo


Pre-Docker 1.13, I found that scaling all services down to 0, waiting for shutdown, then scaling everything back up to the previous level works.

docker service scale mycontainers=0
# wait
docker service scale mycontainers=8
like image 36
Mike Partridge Avatar answered Nov 08 '22 22:11

Mike Partridge


Updating the existing service, swarm will recreate all containers. For example, you can simply update a property of the service to archive restarting.

like image 34
Kane Avatar answered Nov 08 '22 22:11

Kane