Skip to content

Commit

Permalink
Remove broken container check from image prune
Browse files Browse the repository at this point in the history
The imageRefs map was being popualted with containerID, and accessed
with an imageID which would never match.

Remove this broken code because: 1) it hasn't ever worked so isn't
necessary, and 2) because at best it would be racy

ImageDelete() should already handle preventing of removal of used
images.

Signed-off-by: Daniel Nephin <[email protected]>
  • Loading branch information
dnephin committed Feb 8, 2018
1 parent 15001f8 commit 3aa4f7f
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
timetypes "github.com/docker/docker/api/types/time"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
"github.com/docker/docker/pkg/directory"
Expand Down Expand Up @@ -193,16 +194,6 @@ func (daemon *Daemon) ImagesPrune(ctx context.Context, pruneFilters filters.Args
} else {
allImages = daemon.imageStore.Map()
}
allContainers := daemon.List()
imageRefs := map[string]bool{}
for _, c := range allContainers {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
imageRefs[c.ID] = true
}
}

// Filter intermediary images and get their unique size
allLayers := make(map[layer.ChainID]layer.Layer)
Expand Down Expand Up @@ -242,14 +233,8 @@ deleteImagesLoop:
default:
}

dgst := digest.Digest(id)
hex := dgst.Hex()
if _, ok := imageRefs[hex]; ok {
continue
}

deletedImages := []types.ImageDeleteResponseItem{}
refs := daemon.referenceStore.References(dgst)
refs := daemon.referenceStore.References(id.Digest())
if len(refs) > 0 {
shouldDelete := !danglingOnly
if !shouldDelete {
Expand All @@ -268,17 +253,16 @@ deleteImagesLoop:
if shouldDelete {
for _, ref := range refs {
imgDel, err := daemon.ImageDelete(ref.String(), false, true)
if err != nil {
logrus.Warnf("could not delete reference %s: %v", ref.String(), err)
if imageDeleteFailed(ref.String(), err) {
continue
}
deletedImages = append(deletedImages, imgDel...)
}
}
} else {
hex := id.Digest().Hex()
imgDel, err := daemon.ImageDelete(hex, false, true)
if err != nil {
logrus.Warnf("could not delete image %s: %v", hex, err)
if imageDeleteFailed(hex, err) {
continue
}
deletedImages = append(deletedImages, imgDel...)
Expand Down Expand Up @@ -309,6 +293,18 @@ deleteImagesLoop:
return rep, nil
}

func imageDeleteFailed(ref string, err error) bool {
switch {
case err == nil:
return false
case errdefs.IsConflict(err):
return true
default:
logrus.Warnf("failed to prune image %s: %v", ref, err)
return true
}
}

// localNetworksPrune removes unused local networks
func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filters.Args) *types.NetworksPruneReport {
rep := &types.NetworksPruneReport{}
Expand Down

0 comments on commit 3aa4f7f

Please sign in to comment.