Browse code

Fix workdir cache invalidation

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

Tonis Tiigi authored on 2017/01/07 04:55:37
Showing 2 changed files
... ...
@@ -298,17 +298,19 @@ func workdir(b *Builder, args []string, attributes map[string]bool, original str
298 298
 	}
299 299
 	b.runConfig.Image = b.image
300 300
 
301
+	cmd := b.runConfig.Cmd
302
+	comment := "WORKDIR " + b.runConfig.WorkingDir
303
+	// reset the command for cache detection
304
+	b.runConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), "#(nop) "+comment))
305
+	defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
306
+
301 307
 	if hit, err := b.probeCache(); err != nil {
302 308
 		return err
303 309
 	} else if hit {
304 310
 		return nil
305 311
 	}
306 312
 
307
-	// Actually copy the struct
308
-	workdirConfig := *b.runConfig
309
-	workdirConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), fmt.Sprintf("#(nop) WORKDIR %s", b.runConfig.WorkingDir)))
310
-
311
-	container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: &workdirConfig})
313
+	container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig})
312 314
 	if err != nil {
313 315
 		return err
314 316
 	}
... ...
@@ -317,7 +319,7 @@ func workdir(b *Builder, args []string, attributes map[string]bool, original str
317 317
 		return err
318 318
 	}
319 319
 
320
-	return b.commit(container.ID, b.runConfig.Cmd, "WORKDIR "+b.runConfig.WorkingDir)
320
+	return b.commit(container.ID, cmd, comment)
321 321
 }
322 322
 
323 323
 // RUN some command yo
... ...
@@ -7398,6 +7398,10 @@ func (s *DockerSuite) TestBuildWorkdirCmd(c *check.C) {
7398 7398
                 FROM busybox
7399 7399
                 WORKDIR /
7400 7400
                 `
7401
-	_, err := buildImage("testbuildworkdircmd", dockerFile, false)
7401
+	_, err := buildImage("testbuildworkdircmd", dockerFile, true)
7402 7402
 	c.Assert(err, checker.IsNil)
7403
+
7404
+	_, out, err := buildImageWithOut("testbuildworkdircmd", dockerFile, true)
7405
+	c.Assert(err, checker.IsNil)
7406
+	c.Assert(strings.Count(out, "Using cache"), checker.Equals, 1)
7403 7407
 }