From f3ebd798c64cfbea6ecd1cc180d4d57c00dcb7b0 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Wed, 2 Aug 2023 08:33:03 -0400 Subject: [PATCH] Make podman run --rmi automatically set --rm Forcing users to set --rm when setting --rmi is just bad UI. If I want the image to be removed, it implies that I want the container removed that I am creating. Fixes: https://github.com/containers/podman/issues/15640 Signed-off-by: Daniel J Walsh --- cmd/podman/containers/run.go | 9 ++++++++- docs/source/markdown/podman-run.1.md.in | 2 +- test/system/030-run.bats | 6 +++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/podman/containers/run.go b/cmd/podman/containers/run.go index 51fc431b5b..28598187b1 100644 --- a/cmd/podman/containers/run.go +++ b/cmd/podman/containers/run.go @@ -1,6 +1,7 @@ package containers import ( + "errors" "fmt" "os" "strings" @@ -63,7 +64,7 @@ func runFlags(cmd *cobra.Command) { flags.SetNormalizeFunc(utils.AliasFlags) flags.BoolVar(&runOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process") - flags.BoolVar(&runRmi, "rmi", false, "Remove container image unless used by other containers") + flags.BoolVar(&runRmi, "rmi", false, "Remove image unless used by other containers, implies --rm=true") preserveFdsFlagName := "preserve-fds" flags.UintVar(&runOpts.PreserveFDs, "preserve-fds", 0, "Pass a number of additional file descriptors into the container") @@ -110,6 +111,12 @@ func run(cmd *cobra.Command, args []string) error { return err } + if runRmi { + if cmd.Flags().Changed("rm") && !cliVals.Rm { + return errors.New("the --rmi option does not work without --rm=true") + } + cliVals.Rm = true + } // TODO: Breaking change should be made fatal in next major Release if cliVals.TTY && cliVals.Interactive && !term.IsTerminal(int(os.Stdin.Fd())) { logrus.Warnf("The input device is not a TTY. The --tty and --interactive flags might not work properly") diff --git a/docs/source/markdown/podman-run.1.md.in b/docs/source/markdown/podman-run.1.md.in index a42ddd67f7..68181e99b3 100644 --- a/docs/source/markdown/podman-run.1.md.in +++ b/docs/source/markdown/podman-run.1.md.in @@ -347,7 +347,7 @@ the container when it exits. The default is **false**. #### **--rmi** After exit of the container, remove the image unless another -container is using it. The default is *false*. +container is using it. Implies --rm=true on the new container. The default is *false*. @@option rootfs diff --git a/test/system/030-run.bats b/test/system/030-run.bats index 9bcfc3e2d0..3f26624267 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -182,12 +182,16 @@ echo $rand | 0 | $rand # Now try running with --rmi : it should succeed, but not remove the image run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true + is "$output" ".*image is in use by a container" "--rmi should warn that the image was not removed" run_podman image exists $NONLOCAL_IMAGE # Remove the stray container, and run one more time with --rmi. run_podman rm /keepme - run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true + run_podman run --rmi $NONLOCAL_IMAGE /bin/true run_podman 1 image exists $NONLOCAL_IMAGE + + run_podman 125 run --rmi --rm=false $NONLOCAL_IMAGE /bin/true + is "$output" "Error: the --rmi option does not work without --rm=true" "--rmi should refuse to remove images when --rm=false set by user" } # 'run --conmon-pidfile --cid-file' makes sure we don't regress on these flags.