简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM