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 <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2023-08-02 08:33:03 -04:00
parent cd5ce63724
commit f3ebd798c6
3 changed files with 14 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package containers package containers
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -63,7 +64,7 @@ func runFlags(cmd *cobra.Command) {
flags.SetNormalizeFunc(utils.AliasFlags) flags.SetNormalizeFunc(utils.AliasFlags)
flags.BoolVar(&runOpts.SigProxy, "sig-proxy", true, "Proxy received signals to the process") 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" preserveFdsFlagName := "preserve-fds"
flags.UintVar(&runOpts.PreserveFDs, "preserve-fds", 0, "Pass a number of additional file descriptors into the container") 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 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 // TODO: Breaking change should be made fatal in next major Release
if cliVals.TTY && cliVals.Interactive && !term.IsTerminal(int(os.Stdin.Fd())) { 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") logrus.Warnf("The input device is not a TTY. The --tty and --interactive flags might not work properly")

View File

@ -347,7 +347,7 @@ the container when it exits. The default is **false**.
#### **--rmi** #### **--rmi**
After exit of the container, remove the image unless another 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 @@option rootfs

View File

@ -182,12 +182,16 @@ echo $rand | 0 | $rand
# Now try running with --rmi : it should succeed, but not remove the image # Now try running with --rmi : it should succeed, but not remove the image
run_podman run --rmi --rm $NONLOCAL_IMAGE /bin/true 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 run_podman image exists $NONLOCAL_IMAGE
# Remove the stray container, and run one more time with --rmi. # Remove the stray container, and run one more time with --rmi.
run_podman rm /keepme 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 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. # 'run --conmon-pidfile --cid-file' makes sure we don't regress on these flags.