-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Relates to / noticed in moby/moby#41228 (comment)
When using docker rm / docker container rm with the -f / --force option, attempts to remove non-existing containers should print a warning, but should return a zero exit code ("successful").
Currently, a non-zero exit code is returned, marking the removal as "failed";
$ docker rm -fv 798c9471b695
Error: No such container: 798c9471b695
$ echo $?
1
The command should match the behavior of rm / rm -f, with the exception that a warning is printed (instead of silently ignored):
Running rm without -f, files/directories are removed, but the command returns a non-zero exit code:
touch some-file && rm no-such-file some-file; echo exit code: $?; ls -la
# rm: no-such-file: No such file or directory
# exit code: 1
# total 0
# drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:15 .
# drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 ..
mkdir some-directory && rm -r no-such-directory some-directory; echo exit code: $?; ls -la
# rm: no-such-file: No such file or directory
# exit code: 1
# total 0
# drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:15 .
# drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 ..Running rm with -f silences output and returns a zero exit code:
touch some-file && rm -f no-such-file some-file; echo exit code: $?; ls -la
# exit code: 0
# total 0
# drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:17 .
# drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 ..
mkdir some-directory && rm -rf no-such-directory some-directory; echo exit code: $?; ls -la
# exit code: 0
# total 0
# drwxr-xr-x 2 sebastiaan staff 64 Aug 14 12:17 .
# drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 ..Note that other reasons for a delete to fail should still result in a non-zero exit code, matching the behavior of rm. For instance, in the example below, the rm failed because directories can only be removed if the -r option is used;
touch some-file && mkdir some-directory && rm -f some-directory no-such-file some-file; echo exit code: $?; ls -la
# rm: some-directory: is a directory
# exit code: 1
# total 0
# drwxr-xr-x 3 sebastiaan staff 96 Aug 14 14:15 .
# drwxr-xr-x 199 sebastiaan staff 6368 Aug 14 12:13 ..
# drwxr-xr-x 2 sebastiaan staff 64 Aug 14 14:15 some-directoryHandling of images look to be working as intended:
docker tag busybox:latest myimage:latest && docker image rm nosuchimage:latest myimage:latest; echo exit code: $?
# Untagged: myimage:latest
# Error: No such image: nosuchimage:latest
# exit code: 1
docker tag busybox:latest myimage:latest && docker image rm -f nosuchimage:latest myimage:latest; echo exit code: $?
# Untagged: myimage:latest
# Error: No such image: nosuchimage:latest
# exit code: 0But handling of containers is incorrect:
docker create --name mycontainer busybox && docker rm nosuchcontainer mycontainer; echo exit code: $?; docker ps -a --filter name=mycontainer
# df23cc8573f00e97d6e948b48d9ea7d75ce3b4faaab4fe1d3458d3bfa451f39d
# mycontainer
# Error: No such container: nosuchcontainer
# exit code: 1
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker create --name mycontainer busybox && docker rm -f nosuchcontainer mycontainer; echo exit code: $?; docker ps -a --filter name=mycontainer
# 437fbb524ad22d5e051154477a490699712b39e506eb32ca04e0d91433f94fbf
# mycontainer
# Error: No such container: nosuchcontainer
# exit code: 1
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESSummary
Expected behavior when using docker rm -f [container] [container] / docker container rm -f [container] [container]:
- All containers specified are attempted to be deleted (the command does not stop on the first failure)
- If a container does not exist, a warning/error is printed, and the next container is tried. The error itself is ignored. The exit status is not updated (remains zero if there were no other errors)
- If a container fails to be removed for any other reason than it being a non-existing container, the error is printed and the next container is tried. The exit status will be non-zero.