docker / cli

The Docker CLI
Apache License 2.0
4.75k stars 1.88k forks source link

Remove-commands should not fail when the argument list is empty since it is effectively a NOP #1666

Open ghost opened 5 years ago

ghost commented 5 years ago
$ docker --version
Docker version 18.09.1-ce, build 4c52b901c6

Description

Commands docker rm and docker rmi fail, returning an error code and breaking the command chain. Since these commands effectively do nothing if no arguments are given, there is no reason for them to fail.

Use case

$ docker rm $(docker ps -a -q) && docker rmi $(docker images -q)

To remove all the images you need first to remove all the containers but this will fail if there are no containers. The expected effect of docker rm $(docker ps -a -q) is already achieved, so it should not fail. The same thing with docker rmi $(docker images -q).

thaJeztah commented 1 year ago

We need to look at options, and to see what makes sense (and if we can take an equivalent approach on other commands).

Comparing with rm on Linux

rm ""
rm: can't remove '': No such file or directory

rm -f ""
echo $?
0

For rm -f we already produce a "successful" exist status, but still print a warning if the container didn't exist;

docker rm -f nosuchcontainer
Error: No such container: nosuchcontainer
echo $?
0