Browse code

Merge pull request #22868 from Microsoft/jjh/dockerfilecmd

Windows: CMD not honouring arg escaping

Sebastiaan van Stijn authored on 2016/07/08 01:21:26
Showing 3 changed files
... ...
@@ -407,6 +407,8 @@ func cmd(b *Builder, args []string, attributes map[string]bool, original string)
407 407
 	}
408 408
 
409 409
 	b.runConfig.Cmd = strslice.StrSlice(cmdSlice)
410
+	// set config as already being escaped, this prevents double escaping on windows
411
+	b.runConfig.ArgsEscaped = true
410 412
 
411 413
 	if err := b.commit("", b.runConfig.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
412 414
 		return err
... ...
@@ -74,6 +74,7 @@ func merge(userConf, imageConf *containertypes.Config) error {
74 74
 	if len(userConf.Entrypoint) == 0 {
75 75
 		if len(userConf.Cmd) == 0 {
76 76
 			userConf.Cmd = imageConf.Cmd
77
+			userConf.ArgsEscaped = imageConf.ArgsEscaped
77 78
 		}
78 79
 
79 80
 		if userConf.Entrypoint == nil {
... ...
@@ -6960,3 +6960,28 @@ func (s *DockerSuite) TestBuildShellWindowsPowershell(c *check.C) {
6960 6960
 		c.Fatalf("Line with 'John' not found in output %q", out)
6961 6961
 	}
6962 6962
 }
6963
+
6964
+// #22868. Make sure shell-form CMD is marked as escaped in the config of the image
6965
+func (s *DockerSuite) TestBuildCmdShellArgsEscaped(c *check.C) {
6966
+	testRequires(c, DaemonIsWindows)
6967
+	name := "testbuildcmdshellescaped"
6968
+
6969
+	_, err := buildImage(name, `
6970
+  FROM `+minimalBaseImage()+`
6971
+  CMD "tasklist"
6972
+  `, true)
6973
+	if err != nil {
6974
+		c.Fatal(err)
6975
+	}
6976
+	res := inspectFieldJSON(c, name, "Config.ArgsEscaped")
6977
+	if res != "true" {
6978
+		c.Fatalf("CMD did not update Config.ArgsEscaped on image: %v", res)
6979
+	}
6980
+	dockerCmd(c, "run", "--name", "inspectme", name)
6981
+	dockerCmd(c, "wait", "inspectme")
6982
+	res = inspectFieldJSON(c, name, "Config.Cmd")
6983
+
6984
+	if res != `["cmd","/S","/C","\"tasklist\""]` {
6985
+		c.Fatalf("CMD was not escaped Config.Cmd: got %v", res)
6986
+	}
6987
+}