summaryrefslogtreecommitdiff
path: root/libpod/image
diff options
context:
space:
mode:
Diffstat (limited to 'libpod/image')
-rw-r--r--libpod/image/image.go6
-rw-r--r--libpod/image/prune.go39
2 files changed, 33 insertions, 12 deletions
diff --git a/libpod/image/image.go b/libpod/image/image.go
index ea326d820..a32336f49 100644
--- a/libpod/image/image.go
+++ b/libpod/image/image.go
@@ -823,9 +823,9 @@ func (i *Image) Inspect(ctx context.Context) (*inspect.ImageData, error) {
return nil, err
}
- var repoDigests []string
- for _, name := range i.Names() {
- repoDigests = append(repoDigests, strings.SplitN(name, ":", 2)[0]+"@"+i.Digest().String())
+ repoDigests, err := i.RepoDigests()
+ if err != nil {
+ return nil, err
}
driver, err := i.DriverData()
diff --git a/libpod/image/prune.go b/libpod/image/prune.go
index 6a1f160d5..8602c222c 100644
--- a/libpod/image/prune.go
+++ b/libpod/image/prune.go
@@ -1,9 +1,11 @@
package image
+import "github.com/pkg/errors"
+
// GetPruneImages returns a slice of images that have no names/unused
-func (ir *Runtime) GetPruneImages() ([]*Image, error) {
+func (ir *Runtime) GetPruneImages(all bool) ([]*Image, error) {
var (
- unamedImages []*Image
+ pruneImages []*Image
)
allImages, err := ir.GetImages()
if err != nil {
@@ -11,16 +13,35 @@ func (ir *Runtime) GetPruneImages() ([]*Image, error) {
}
for _, i := range allImages {
if len(i.Names()) == 0 {
- unamedImages = append(unamedImages, i)
+ pruneImages = append(pruneImages, i)
continue
}
- containers, err := i.Containers()
- if err != nil {
- return nil, err
+ if all {
+ containers, err := i.Containers()
+ if err != nil {
+ return nil, err
+ }
+ if len(containers) < 1 {
+ pruneImages = append(pruneImages, i)
+ }
}
- if len(containers) < 1 {
- unamedImages = append(unamedImages, i)
+ }
+ return pruneImages, nil
+}
+
+// PruneImages prunes dangling and optionally all unused images from the local
+// image store
+func (ir *Runtime) PruneImages(all bool) ([]string, error) {
+ var prunedCids []string
+ pruneImages, err := ir.GetPruneImages(all)
+ if err != nil {
+ return nil, errors.Wrap(err, "unable to get images to prune")
+ }
+ for _, p := range pruneImages {
+ if err := p.Remove(true); err != nil {
+ return nil, errors.Wrap(err, "failed to prune image")
}
+ prunedCids = append(prunedCids, p.ID())
}
- return unamedImages, nil
+ return prunedCids, nil
}