3

I've seen many times the follwoing command to stop all docker containers:

docker stop $(docker ps -a -q)

There are two things that are not clear to me:

  1. docker ps -a prints all containers, not only running ones, so what is the point to stop containers that are already stopped?
  2. If docker ps returns/prints nothing (there are no running images) then docker stop blaims that it's not enough arguments.

What do I miss here? What is the best approach to cleanup an environment after docker?

Oleksandr Taran
  • 581
  • 5
  • 9

2 Answers2

3

use this will not run if the docker ps is empty:

docker ps -q | xargs --no-run-if-empty docker stop

normally you use rm and system prune if you really want to cleanup

LinPy
  • 16,987
  • 4
  • 43
  • 57
0

You can also use this oneliner:

docker stop $(docker ps -aq | tr '\n' ' ')
Evhz
  • 8,852
  • 9
  • 51
  • 69