mirror of
https://github.com/containers/podman.git
synced 2025-06-24 03:08:13 +08:00
Merge pull request #2281 from rhatdan/deleteContainer
Remove container from storage on --force
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||||
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
"github.com/containers/libpod/cmd/podman/libpodruntime"
|
||||||
"github.com/containers/libpod/cmd/podman/shared"
|
"github.com/containers/libpod/cmd/podman/shared"
|
||||||
|
"github.com/containers/libpod/libpod"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -61,10 +62,18 @@ func rmCmd(c *cliconfig.RmValues) error {
|
|||||||
|
|
||||||
delContainers, err := getAllOrLatestContainers(&c.PodmanCommand, runtime, -1, "all")
|
delContainers, err := getAllOrLatestContainers(&c.PodmanCommand, runtime, -1, "all")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if c.Force && len(c.InputArgs) > 0 {
|
||||||
|
if errors.Cause(err) == libpod.ErrNoSuchCtr {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
runtime.RemoveContainersFromStorage(c.InputArgs)
|
||||||
|
}
|
||||||
if len(delContainers) == 0 {
|
if len(delContainers) == 0 {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(err.Error())
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, container := range delContainers {
|
for _, container := range delContainers {
|
||||||
|
@ -17,7 +17,9 @@ Remove all containers. Can be used in conjunction with -f as well.
|
|||||||
|
|
||||||
**--force, f**
|
**--force, f**
|
||||||
|
|
||||||
Force the removal of a running and paused containers
|
Force the removal of running and paused containers. Forcing a containers removal also
|
||||||
|
removes containers from container storage even if the container is not known to podman.
|
||||||
|
Containers could have been created by a different container engine.
|
||||||
|
|
||||||
**--latest, -l**
|
**--latest, -l**
|
||||||
|
|
||||||
|
@ -2,15 +2,20 @@ package libpod
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/containers/libpod/libpod/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrNoSuchCtr indicates the requested container does not exist
|
// ErrNoSuchCtr indicates the requested container does not exist
|
||||||
ErrNoSuchCtr = errors.New("no such container")
|
ErrNoSuchCtr = image.ErrNoSuchCtr
|
||||||
|
|
||||||
// ErrNoSuchPod indicates the requested pod does not exist
|
// ErrNoSuchPod indicates the requested pod does not exist
|
||||||
ErrNoSuchPod = errors.New("no such pod")
|
ErrNoSuchPod = image.ErrNoSuchPod
|
||||||
|
|
||||||
// ErrNoSuchImage indicates the requested image does not exist
|
// ErrNoSuchImage indicates the requested image does not exist
|
||||||
ErrNoSuchImage = errors.New("no such image")
|
ErrNoSuchImage = image.ErrNoSuchImage
|
||||||
|
|
||||||
// ErrNoSuchVolume indicates the requested volume does not exist
|
// ErrNoSuchVolume indicates the requested volume does not exist
|
||||||
ErrNoSuchVolume = errors.New("no such volume")
|
ErrNoSuchVolume = errors.New("no such volume")
|
||||||
|
|
||||||
|
@ -10,7 +10,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/containers/libpod/libpod/image"
|
||||||
"github.com/containers/libpod/pkg/rootless"
|
"github.com/containers/libpod/pkg/rootless"
|
||||||
|
"github.com/containers/storage"
|
||||||
"github.com/containers/storage/pkg/stringid"
|
"github.com/containers/storage/pkg/stringid"
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -564,3 +566,16 @@ func (r *Runtime) Export(name string, path string) error {
|
|||||||
return ctr.Export(path)
|
return ctr.Export(path)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveContainersFromStorage attempt to remove containers from storage that do not exist in libpod database
|
||||||
|
func (r *Runtime) RemoveContainersFromStorage(ctrs []string) {
|
||||||
|
for _, i := range ctrs {
|
||||||
|
// if the container does not exist in database, attempt to remove it from storage
|
||||||
|
if _, err := r.LookupContainer(i); err != nil && errors.Cause(err) == image.ErrNoSuchCtr {
|
||||||
|
r.storageService.UnmountContainerImage(i, true)
|
||||||
|
if err := r.storageService.DeleteContainer(i); err != nil && errors.Cause(err) != storage.ErrContainerUnknown {
|
||||||
|
logrus.Errorf("Failed to remove container %q from storage: %s", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user