DockerCLI/cli/command
Sebastiaan van Stijn 9a071a993a
Return zero exit-code when force-removing non-existing containers
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` 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

This patch updates the `docker rm` / `docker container rm` command to not produce
an error when attempting to remove a missing containers, and instead only print
the error, but return a zero (0) exit code.

With this patch applied:

    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: 0
    # CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-14 16:17:40 +02:00
..
builder Fix builder prune -a/--all flag description 2020-02-18 14:30:28 +01:00
checkpoint Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
commands Remove "docker engine" subcommands 2019-12-12 17:51:25 +01:00
config Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
container Return zero exit-code when force-removing non-existing containers 2020-08-14 16:17:40 +02:00
context bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
formatter cli/formatter: reformat TestImageContext test cases 2020-07-16 11:34:52 +02:00
idresolver bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
image support emulated version detection 2020-07-30 11:38:29 -07:00
inspect bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
manifest Added support for setting OS version in docker manifest annotate. 2020-06-12 12:04:03 -07:00
network Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
node Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
plugin Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
registry Don't filter out registries to logout from with config file contents 2020-06-15 14:29:37 +02:00
secret Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
service Fix order of processing of some xx-add/xx-rm service update flags 2020-08-04 19:09:59 +02:00
stack TestServiceUpdateResolveImageChanged: use subtests 2020-07-06 14:58:40 +02:00
swarm Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
system support emulated version detection 2020-07-30 11:38:29 -07:00
task bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
testdata Dynamically register kubernetes context store endpoint type. 2019-05-20 13:28:11 +01:00
trust Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
volume Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
cli.go Fix bug with panic when DOCKER_CLI_EXPERIMENTAL environment variable is incorrect 2020-05-24 23:21:20 +03:00
cli_options.go update docker, replace github.com/docker/pkg/term, github.com/docker/pkg/mount 2020-04-22 17:16:13 +02:00
cli_options_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
cli_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
context.go Don't loose additional metadata fields 2020-06-10 15:07:23 +02:00
context_test.go Don't loose additional metadata fields 2020-06-10 15:07:23 +02:00
defaultcontextstore.go Push check for kubernetes requirement down into the endpoint 2019-05-20 13:28:11 +01:00
defaultcontextstore_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
events_utils.go updated vendoring 2017-09-01 19:41:06 -04:00
orchestrator.go Fast Context Switch: commands 2019-01-10 22:25:43 +01:00
orchestrator_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
registry.go update docker, replace github.com/docker/pkg/term, github.com/docker/pkg/mount 2020-04-22 17:16:13 +02:00
registry_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
streams.go Extract streams helpers from command package to their own package to remove a cyclic dependency from command to internal/containerizedengine 2019-01-28 14:36:00 +01:00
trust.go Refactor content_trust cli/flags handling 2018-03-08 15:00:43 -05:00
utils.go docker cp: prevent NPE when failing to stat destination 2019-12-09 15:32:02 +01:00
utils_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00