136

How to restart all running docker containers? Mainly looking for a shortcut instead of doing

docker restart containerid1 containerid2

Ranjith's
  • 4,508
  • 5
  • 24
  • 40

9 Answers9

287

Just run

docker restart $(docker ps -q)

Update

For restarting ALL (stopped and running) containers use docker restart $(docker ps -a -q) as in answer lower.

Andrey Romashin
  • 3,194
  • 1
  • 12
  • 15
  • 1
    I know `-a` lists containers not running, but what does `-q` stand for? – Jim Aho May 18 '17 at 18:08
  • from `docker ps --help`: `-q, --quiet Only display numeric IDs` You can get help for every docker command by command `docker [command] --help` – Andrey Romashin May 19 '17 at 06:56
  • this attempts to start a container for every value in the table output of `docker ps --all`, so it will try to run `docker restart 'up for 2 weeks ago'`, `docker restart '33060/tcp'` and so on. Better to somehow limit the output of the `docker ps` command to just the name column. For example `docker restart $(docker ps --all --format "{{.Names}}")` – Brian H. Jun 17 '21 at 16:25
  • 1
    -1 because the updated answer restarts _**all containers**_ whereas the original question is to restart _all **running** containers_. – John Rocha Dec 10 '21 at 18:52
  • You are right John. I corrected my updated answer. – Andrey Romashin Dec 15 '21 at 09:01
  • For me, this command gave the following error: "docker restart" requires at least 1 argument. Adding '-a' option along with '-q' option worked. – Karthik Rao Jun 09 '22 at 07:05
106

For me its now :

docker restart $(docker ps -a -q)
bohr
  • 1,206
  • 1
  • 9
  • 11
30

If you have docker-compose, all you need to do is:

docker-compose restart 

And you get nice print out of the container's name along with its status of the restart (done/error)

Here is the official guide for installing: https://docs.docker.com/compose/install/

Asclepius
  • 57,944
  • 17
  • 167
  • 143
benjaminz
  • 3,118
  • 3
  • 35
  • 47
  • I have multiple projects running on a VPS and sometimes after a restart some of them have troubles starting and I'd have to move to every docker project and do that but other answers work to start ALL of them, not only one. – Herii May 17 '22 at 16:20
27

To start only stopped containers:

docker start $(docker ps -a -q -f status=exited)

(On windows it works in Powershell).

Cepr0
  • 28,144
  • 8
  • 75
  • 101
12

To start all the containers:

  docker restart $(docker ps -a -q)

Use sudo if you don't have permission to perform this:

sudo docker restart $(sudo docker ps -a -q)
Hitesh Kumar
  • 193
  • 3
  • 11
1

To restart all the online docker containers

docker restart $(docker ps -a -q -f status=healthy)

To start all the stopped containers:

docker start $(docker ps -a -q -f status=exited)

Other status options can be:

created, restarting, running, removing, paused, exited, dead.

0

To start multiple containers with the only particular container ids $ docker restart container-id1 container-id2 container-id3 ...

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
0

An other way docker ps -q | xargs docker restart

-1

start last 5 containers

 docker start $(docker ps -n 5 -q)
WagnerVF
  • 1
  • 1