Browse code

Fix workdir cache invalidation

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit e16086005425b626bccc0fedcf5ae195f913c636)
Signed-off-by: Victor Vieux <vieux@docker.com>

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