0

So, I have the following command

$(docker container ls -a -q) | docker container stop

to stop all my docker containers, which works great! Except when I've just wiped clean all of them:

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

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

Stop one or more running containers

Is there any way I can prevent this error message from showing up using a single line command?

Note: my original command was actually docker container stop $(docker container ls -a -q), which gives the same error message, but I tought it would be easier to describe my problem using pipe/chain one command to the other. Answers for both commands are OK, but I'll prefer to use docker container stop $(docker container ls -a -q)

2
  • 1
    I'm pretty certain $(docker container ls -a -q) | docker container stop doesn't work great at all. At the very least if you wanted to use a pipe that should be docker container ls -a -q | xargs docker container stop
    – muru
    May 11 at 2:31
  • And for xargs there's unix.stackexchange.com/q/521595/70524
    – muru
    May 11 at 3:21

2 Answers 2

3

Since the N-ary stop operation of Docker is silly with regard to zero containers, what you can do is treat docker stop container as as a unary operation which requires exactly one argument, and do your own distribution of a list of terms over that unary operation. Of course, such a way that your logic handles the empty sequence of terms:

for x in $(docker container ls -a -q); do docker container stop $x; done

If $(docker ...) produces nothing, the loop doesn't execute.

1

You can silence shell output using redirects, see this post for more details: https://stackoverflow.com/questions/2292847/how-to-silence-output-in-a-bash-script

I might use something more like this to handle the response so I still get output.

docker container stop $(docker container ls -a -q) >/dev/null 2>&1 || echo "Nothing to stop."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.