diff --git a/buildfile.go b/buildfile.go index 05dafe53e1..60a975c8de 100644 --- a/buildfile.go +++ b/buildfile.go @@ -98,12 +98,14 @@ func (b *buildFile) CmdRun(args string) error { utils.Debugf("Commang to be executed: %v", b.config.Cmd) - if cache, err := b.srv.ImageGetCached(b.image, config); err != nil { + if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil { return err } else if cache != nil { - utils.Debugf("Use cached version") + utils.Debugf("[BUILDER] Use cached version") b.image = cache.Id return nil + } else { + utils.Debugf("[BUILDER] Cache miss") } cid, err := b.run() @@ -182,7 +184,11 @@ func (b *buildFile) CmdInsert(args string) error { if err := container.Inject(file.Body, destPath); err != nil { return err } - return b.commit(cid, cmd, fmt.Sprintf("INSERT %s in %s", sourceUrl, destPath)) + if err := b.commit(cid, cmd, fmt.Sprintf("INSERT %s in %s", sourceUrl, destPath)); err != nil { + return err + } + b.config.Cmd = cmd + return nil } func (b *buildFile) CmdAdd(args string) error { @@ -271,6 +277,17 @@ func (b *buildFile) commit(id string, autoCmd []string, comment string) error { b.config.Image = b.image if id == "" { b.config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment} + + if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil { + return err + } else if cache != nil { + utils.Debugf("[BUILDER] Use cached version") + b.image = cache.Id + return nil + } else { + utils.Debugf("[BUILDER] Cache miss") + } + if cid, err := b.run(); err != nil { return err } else { diff --git a/server.go b/server.go index 0440b0a8a4..0125d2f0c1 100644 --- a/server.go +++ b/server.go @@ -720,28 +720,13 @@ func (srv *Server) ImageDelete(name string) error { } func (srv *Server) ImageGetCached(imgId string, config *Config) (*Image, error) { - - // Retrieve all images - images, err := srv.runtime.graph.All() + byParent, err := srv.runtime.graph.ByParent() if err != nil { return nil, err } - // Store the tree in a map of map (map[parentId][childId]) - imageMap := make(map[string]map[string]struct{}) - for _, img := range images { - if _, exists := imageMap[img.Parent]; !exists { - imageMap[img.Parent] = make(map[string]struct{}) - } - imageMap[img.Parent][img.Id] = struct{}{} - } - // Loop on the children of the given image and check the config - for elem := range imageMap[imgId] { - img, err := srv.runtime.graph.Get(elem) - if err != nil { - return nil, err - } + for _, img := range byParent[imgId] { if CompareConfig(&img.ContainerConfig, config) { return img, nil }