diff --git a/daemon/daemon.go b/daemon/daemon.go index 91af20ac66..3dda37ff34 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -1147,6 +1147,14 @@ func (daemon *Daemon) GetRemappedUIDGID() (int, int) { // created. nil is returned if a child cannot be found. An error is // returned if the parent image cannot be found. func (daemon *Daemon) ImageGetCached(imgID string, config *runconfig.Config) (*image.Image, error) { + // for now just exit if imgID has no children. + // maybe parentRefs in graph could be used to store + // the Image obj children for faster lookup below but this can + // be quite memory hungry. + if !daemon.Graph().HasChildren(imgID) { + return nil, nil + } + // Retrieve all images images := daemon.Graph().Map() diff --git a/daemon/image_delete.go b/daemon/image_delete.go index 070ec0b1d4..b28072103a 100644 --- a/daemon/image_delete.go +++ b/daemon/image_delete.go @@ -337,5 +337,5 @@ func (daemon *Daemon) checkImageDeleteSoftConflict(img *image.Image) *imageDelet // that there are no repository references to the given image and it has no // child images. func (daemon *Daemon) imageIsDangling(img *image.Image) bool { - return !(daemon.Repositories().HasReferences(img) || daemon.Graph().HasChildren(img.ID)) + return !(daemon.repositories.HasReferences(img) || daemon.Graph().HasChildren(img.ID)) } diff --git a/graph/graph.go b/graph/graph.go index 7ae1807608..9704ede6bd 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -512,8 +512,7 @@ func (graph *Graph) Release(sessionID string, layerIDs ...string) { func (graph *Graph) Heads() map[string]*image.Image { heads := make(map[string]*image.Image) graph.walkAll(func(image *image.Image) { - // If it's not in the byParent lookup table, then - // it's not a parent -> so it's a head! + // if it has no children, then it's not a parent, so it's an head if !graph.HasChildren(image.ID) { heads[image.ID] = image }