mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fix cache miss issue within docker build
This commit is contained in:
parent
b6165daa77
commit
560a74af15
2 changed files with 22 additions and 20 deletions
23
buildfile.go
23
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 {
|
||||
|
|
19
server.go
19
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue