Переглянути джерело

Merge pull request #29956 from tonistiigi/fix-workdir-cache

Fix workdir cache invalidation
Doug Davis 8 роки тому
батько
коміт
ce814b2019

+ 8 - 6
builder/dockerfile/dispatchers.go

@@ -298,17 +298,19 @@ func workdir(b *Builder, args []string, attributes map[string]bool, original str
 	}
 	}
 	b.runConfig.Image = b.image
 	b.runConfig.Image = b.image
 
 
+	cmd := b.runConfig.Cmd
+	comment := "WORKDIR " + b.runConfig.WorkingDir
+	// reset the command for cache detection
+	b.runConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), "#(nop) "+comment))
+	defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
+
 	if hit, err := b.probeCache(); err != nil {
 	if hit, err := b.probeCache(); err != nil {
 		return err
 		return err
 	} else if hit {
 	} else if hit {
 		return nil
 		return nil
 	}
 	}
 
 
-	// Actually copy the struct
-	workdirConfig := *b.runConfig
-	workdirConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), fmt.Sprintf("#(nop) WORKDIR %s", b.runConfig.WorkingDir)))
-
-	container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: &workdirConfig})
+	container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig})
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
@@ -317,7 +319,7 @@ func workdir(b *Builder, args []string, attributes map[string]bool, original str
 		return err
 		return err
 	}
 	}
 
 
-	return b.commit(container.ID, b.runConfig.Cmd, "WORKDIR "+b.runConfig.WorkingDir)
+	return b.commit(container.ID, cmd, comment)
 }
 }
 
 
 // RUN some command yo
 // RUN some command yo

+ 5 - 1
integration-cli/docker_cli_build_test.go

@@ -7398,6 +7398,10 @@ func (s *DockerSuite) TestBuildWorkdirCmd(c *check.C) {
                 FROM busybox
                 FROM busybox
                 WORKDIR /
                 WORKDIR /
                 `
                 `
-	_, err := buildImage("testbuildworkdircmd", dockerFile, false)
+	_, err := buildImage("testbuildworkdircmd", dockerFile, true)
 	c.Assert(err, checker.IsNil)
 	c.Assert(err, checker.IsNil)
+
+	_, out, err := buildImageWithOut("testbuildworkdircmd", dockerFile, true)
+	c.Assert(err, checker.IsNil)
+	c.Assert(strings.Count(out, "Using cache"), checker.Equals, 1)
 }
 }