diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-03-04 10:58:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-04 10:58:10 -0500 |
commit | 90eef95cb1591f20f5716225cf9b2c8c415fc3fc (patch) | |
tree | 9f19d9affffb9c57b5888e2fb3d12b9c642d4a73 | |
parent | a8bc423e2989ceff1ee2ca79d6c1bd8314901e11 (diff) | |
parent | 11e5c53d1191f7a324f212aa2ca2ec2aad1b4676 (diff) | |
download | podman-90eef95cb1591f20f5716225cf9b2c8c415fc3fc.tar.gz podman-90eef95cb1591f20f5716225cf9b2c8c415fc3fc.tar.bz2 podman-90eef95cb1591f20f5716225cf9b2c8c415fc3fc.zip |
Merge pull request #4772 from boaz0/closes_4628
Add the rmi flag to podman-run to delete container image
-rw-r--r-- | cmd/podman/cleanup.go | 1 | ||||
-rw-r--r-- | cmd/podman/cliconfig/config.go | 7 | ||||
-rw-r--r-- | cmd/podman/run.go | 10 | ||||
-rw-r--r-- | completions/bash/podman | 1 | ||||
-rw-r--r-- | docs/source/markdown/podman-container-cleanup.1.md | 8 | ||||
-rw-r--r-- | docs/source/markdown/podman-run.1.md | 5 | ||||
-rw-r--r-- | pkg/adapter/containers.go | 19 | ||||
-rw-r--r-- | pkg/spec/createconfig.go | 5 | ||||
-rw-r--r-- | test/system/030-run.bats | 17 |
9 files changed, 70 insertions, 3 deletions
diff --git a/cmd/podman/cleanup.go b/cmd/podman/cleanup.go index a8bc0c116..80a19b000 100644 --- a/cmd/podman/cleanup.go +++ b/cmd/podman/cleanup.go @@ -44,6 +44,7 @@ func init() { flags.BoolVarP(&cleanupCommand.All, "all", "a", false, "Cleans up all containers") flags.BoolVarP(&cleanupCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of") flags.BoolVar(&cleanupCommand.Remove, "rm", false, "After cleanup, remove the container entirely") + flags.BoolVar(&cleanupCommand.RemoveImage, "rmi", false, "After cleanup, remove the image entirely") markFlagHiddenForRemoteClient("latest", flags) } diff --git a/cmd/podman/cliconfig/config.go b/cmd/podman/cliconfig/config.go index ccc30c603..79917946a 100644 --- a/cmd/podman/cliconfig/config.go +++ b/cmd/podman/cliconfig/config.go @@ -658,9 +658,10 @@ type VolumeRmValues struct { type CleanupValues struct { PodmanCommand - All bool - Latest bool - Remove bool + All bool + Latest bool + Remove bool + RemoveImage bool } type SystemPruneValues struct { diff --git a/cmd/podman/run.go b/cmd/podman/run.go index 219f057c3..27247c5b5 100644 --- a/cmd/podman/run.go +++ b/cmd/podman/run.go @@ -7,6 +7,7 @@ import ( "github.com/containers/libpod/pkg/adapter" "github.com/opentracing/opentracing-go" "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -38,6 +39,7 @@ func init() { flags.SetInterspersed(false) flags.SetNormalizeFunc(aliasFlags) flags.Bool("sig-proxy", true, "Proxy received signals to the process") + flags.Bool("rmi", false, "Remove container image unless used by other containers") flags.AddFlagSet(getNetFlags()) getCreateFlags(&runCommand.PodmanCommand) markFlagHiddenForRemoteClient("authfile", flags) @@ -64,5 +66,13 @@ func runCmd(c *cliconfig.RunValues) error { defer runtime.DeferredShutdown(false) exitCode, err = runtime.Run(getContext(), c, exitCode) + if c.Bool("rmi") { + imageName := c.InputArgs[0] + if newImage, newImageErr := runtime.NewImageFromLocal(imageName); newImageErr != nil { + logrus.Errorf("%s", errors.Wrapf(newImageErr, "failed creating image object")) + } else if _, errImage := runtime.RemoveImage(getContext(), newImage, false); errImage != nil { + logrus.Errorf("%s", errors.Wrapf(errImage, "failed removing image")) + } + } return err } diff --git a/completions/bash/podman b/completions/bash/podman index 13be64e06..895659fe5 100644 --- a/completions/bash/podman +++ b/completions/bash/podman @@ -1967,6 +1967,7 @@ _podman_container_run() { boolean_options="$boolean_options --detach -d --rm + --rmi --sig-proxy=false " __podman_complete_detach_keys && return diff --git a/docs/source/markdown/podman-container-cleanup.1.md b/docs/source/markdown/podman-container-cleanup.1.md index 69e21ce9f..86e6b4316 100644 --- a/docs/source/markdown/podman-container-cleanup.1.md +++ b/docs/source/markdown/podman-container-cleanup.1.md @@ -22,6 +22,14 @@ to run containers such as CRI-O, the last started container could be from either The latest option is not supported on the remote client. +**--rm** + +After cleanup, remove the container entirely. + +**--rmi** + +After cleanup, remove the image entirely. + ## EXAMPLE `podman container cleanup mywebserver` diff --git a/docs/source/markdown/podman-run.1.md b/docs/source/markdown/podman-run.1.md index 220b32a46..bc99a83ca 100644 --- a/docs/source/markdown/podman-run.1.md +++ b/docs/source/markdown/podman-run.1.md @@ -689,6 +689,11 @@ Note that the container will not be removed when it could not be created or started successfully. This allows the user to inspect the container after failure. +**--rmi**=*true|false* + +After exit of the container, remove the image unless another +container is using it. The default is *false*. + **--rootfs** If specified, the first argument refers to an exploded container on the file system. diff --git a/pkg/adapter/containers.go b/pkg/adapter/containers.go index a5e668b04..a5242270e 100644 --- a/pkg/adapter/containers.go +++ b/pkg/adapter/containers.go @@ -1112,6 +1112,15 @@ func (r *LocalRuntime) CleanupContainers(ctx context.Context, cli *cliconfig.Cle } else { failures[ctr.ID()] = err } + + if cli.RemoveImage { + _, imageName := ctr.Image() + if err := removeContainerImage(ctx, ctr, r); err != nil { + failures[imageName] = err + } else { + ok = append(ok, imageName) + } + } } return ok, failures, nil } @@ -1131,6 +1140,16 @@ func cleanupContainer(ctx context.Context, ctr *libpod.Container, runtime *Local return nil } +func removeContainerImage(ctx context.Context, ctr *libpod.Container, runtime *LocalRuntime) error { + _, imageName := ctr.Image() + ctrImage, err := runtime.NewImageFromLocal(imageName) + if err != nil { + return err + } + _, err = runtime.RemoveImage(ctx, ctrImage, false) + return err +} + // Port displays port information about existing containers func (r *LocalRuntime) Port(c *cliconfig.PortValues) ([]*Container, error) { var ( diff --git a/pkg/spec/createconfig.go b/pkg/spec/createconfig.go index 1d9633bb3..9b2255d61 100644 --- a/pkg/spec/createconfig.go +++ b/pkg/spec/createconfig.go @@ -157,6 +157,7 @@ type CreateConfig struct { Resources CreateResourceConfig RestartPolicy string Rm bool //rm + Rmi bool //rmi StopSignal syscall.Signal // stop-signal StopTimeout uint // stop-timeout Systemd bool @@ -234,6 +235,10 @@ func (c *CreateConfig) createExitCommand(runtime *libpod.Runtime) ([]string, err command = append(command, "--rm") } + if c.Rmi { + command = append(command, "--rmi") + } + return command, nil } diff --git a/test/system/030-run.bats b/test/system/030-run.bats index f1e9776c1..b89c76981 100644 --- a/test/system/030-run.bats +++ b/test/system/030-run.bats @@ -136,4 +136,21 @@ echo $rand | 0 | $rand run_podman rmi busybox } +# 'run --rmi' deletes the image in the end unless it's used by another container. +@test "podman run --rmi - remove image" { + skip_if_remote "podman-remote does not emit 'Trying to pull' msgs" + run_podman 0 run --rmi --rm redis /bin/true + run_podman 1 image exists redis +} + + +@test "podman run --rmi - not remove image" { + skip_if_remote "podman-remote does not emit 'Trying to pull' msgs" + run_podman run redis /bin/true + run_podman images | grep redis + run_podman run --rmi --rm redis /bin/true + run_podman images | grep redis + run_podman 0 rm -a +} + # vim: filetype=sh |