Docker Community Forums

Share and learn in the Docker community.

Implement --no-run-if-empty or similar feature in the docker command


(Gabipetrovay) #1

It would be very useful to be able to suppress errors when executing nested docker commands where the inner docker command returns an empty list. For example:

docker rm $(docker ps -a -q)

would exit with an error if docker ps will not find any containers. To suppress this error the user must use xargs:

docker ps -aq | xargs --no-run-if-empty docker rm

It would be more elegant to be able to control this only using a docker command flag like:

docker rm --no-run-if-empty $(docker ps -a -q)

or a similarly named flag.

More examples can be found in this SO question: