mirror of
https://github.com/containers/podman.git
synced 2025-08-06 19:44:14 +08:00
image prune: remove all candidates
Make sure to remove images until there's nothing left to prune. A single iteration may not be sufficient. Fixes: #7872 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
@ -125,29 +125,39 @@ func (ir *Runtime) PruneImages(ctx context.Context, all bool, filter []string) (
|
|||||||
filterFuncs = append(filterFuncs, generatedFunc)
|
filterFuncs = append(filterFuncs, generatedFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
pruneImages, err := ir.GetPruneImages(ctx, all, filterFuncs)
|
pruned := []string{}
|
||||||
if err != nil {
|
prev := 0
|
||||||
return nil, errors.Wrap(err, "unable to get images to prune")
|
for {
|
||||||
}
|
toPrune, err := ir.GetPruneImages(ctx, all, filterFuncs)
|
||||||
prunedCids := make([]string, 0, len(pruneImages))
|
|
||||||
for _, p := range pruneImages {
|
|
||||||
repotags, err := p.RepoTags()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrap(err, "unable to get images to prune")
|
||||||
}
|
}
|
||||||
if err := p.Remove(ctx, true); err != nil {
|
numImages := len(toPrune)
|
||||||
if errors.Cause(err) == storage.ErrImageUsedByContainer {
|
if numImages == 0 || numImages == prev {
|
||||||
logrus.Warnf("Failed to prune image %s as it is in use: %v.\nA container associated with containers/storage i.e. Buildah, CRI-O, etc., maybe associated with this image.\nUsing the rmi command with the --force option will remove the container and image, but may cause failures for other dependent systems.", p.ID(), err)
|
// If there's nothing left to do, return.
|
||||||
continue
|
break
|
||||||
|
}
|
||||||
|
prev = numImages
|
||||||
|
for _, img := range toPrune {
|
||||||
|
repotags, err := img.RepoTags()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil, errors.Wrap(err, "failed to prune image")
|
if err := img.Remove(ctx, false); err != nil {
|
||||||
|
if errors.Cause(err) == storage.ErrImageUsedByContainer {
|
||||||
|
logrus.Warnf("Failed to prune image %s as it is in use: %v.\nA container associated with containers/storage (e.g., Buildah, CRI-O, etc.) maybe associated with this image.\nUsing the rmi command with the --force option will remove the container and image, but may cause failures for other dependent systems.", img.ID(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, errors.Wrap(err, "failed to prune image")
|
||||||
|
}
|
||||||
|
defer img.newImageEvent(events.Prune)
|
||||||
|
nameOrID := img.ID()
|
||||||
|
if len(repotags) > 0 {
|
||||||
|
nameOrID = repotags[0]
|
||||||
|
}
|
||||||
|
pruned = append(pruned, nameOrID)
|
||||||
}
|
}
|
||||||
defer p.newImageEvent(events.Prune)
|
|
||||||
nameOrID := p.ID()
|
|
||||||
if len(repotags) > 0 {
|
|
||||||
nameOrID = repotags[0]
|
|
||||||
}
|
|
||||||
prunedCids = append(prunedCids, nameOrID)
|
|
||||||
}
|
}
|
||||||
return prunedCids, nil
|
return pruned, nil
|
||||||
}
|
}
|
||||||
|
@ -46,11 +46,7 @@ func (ir *ImageEngine) Exists(_ context.Context, nameOrID string) (*entities.Boo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ir *ImageEngine) Prune(ctx context.Context, opts entities.ImagePruneOptions) (*entities.ImagePruneReport, error) {
|
func (ir *ImageEngine) Prune(ctx context.Context, opts entities.ImagePruneOptions) (*entities.ImagePruneReport, error) {
|
||||||
return ir.pruneImagesHelper(ctx, opts.All, opts.Filter)
|
results, err := ir.Libpod.ImageRuntime().PruneImages(ctx, opts.All, opts.Filter)
|
||||||
}
|
|
||||||
|
|
||||||
func (ir *ImageEngine) pruneImagesHelper(ctx context.Context, all bool, filters []string) (*entities.ImagePruneReport, error) {
|
|
||||||
results, err := ir.Libpod.ImageRuntime().PruneImages(ctx, all, filters)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user