Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker rm --force should return 0 (zero) exit-code if container doesn't exist #2677

Closed
thaJeztah opened this issue Aug 14, 2020 · 0 comments · Fixed by #2678
Closed

docker rm --force should return 0 (zero) exit-code if container doesn't exist #2677

thaJeztah opened this issue Aug 14, 2020 · 0 comments · Fixed by #2678

Comments

@thaJeztah
Copy link
Member

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-directory

Handling 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: 0

But 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               NAMES

Summary

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant