Gabriel Petrovay
Gabriel Petrovay

Reputation: 21864

Is there a flag to silence docker when an empty list is provided

Such a feature is useful when running multiple docker commands in one that follow this pattern:

docker do_smth $(docker query_smth)

For example:

docker stop $(docker ps -q)

or

docker rm $(docker ps -a -q)

or

docker network rm $(docker inspect ... --format ...)

If the inner docker command returns an empty list, the outer command will fail because and will display the help.

"docker stop" requires at least 1 argument.
See 'docker stop --help'.

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...] [flags]

Stop one or more running containers

Is there a way to silence docker or make docker not complain on empty lists? Something like: "Kill everybody. If there is no one, job done."

This would be similar to mkdir -p exiting_directory vs mkdir exiting_directory where the former will not complain if the directories exist.

Upvotes: 1

Views: 1114

Answers (1)

BMitch
BMitch

Reputation: 263637

For scripting where the result may be empty, I prefer to use xargs --no-run-if-empty:

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

Upvotes: 6

Related Questions