Browse code

Inherit Cmd only if no --entrypoint specified on run

Fixes #5147
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)

Alexandr Morozov authored on 2014/07/10 14:46:11
Showing 3 changed files
... ...
@@ -71,7 +71,7 @@ type buildFile struct {
71 71
 	outOld io.Writer
72 72
 	sf     *utils.StreamFormatter
73 73
 
74
-	// cmdSet indicates is CMD was setted in current Dockerfile
74
+	// cmdSet indicates is CMD was set in current Dockerfile
75 75
 	cmdSet bool
76 76
 }
77 77
 
... ...
@@ -202,7 +202,8 @@ func (b *buildFile) CmdRun(args string) error {
202 202
 	}
203 203
 
204 204
 	cmd := b.config.Cmd
205
-	b.config.Cmd = nil
205
+	// set Cmd manually, this is special case only for Dockerfiles
206
+	b.config.Cmd = config.Cmd
206 207
 	runconfig.Merge(b.config, config)
207 208
 
208 209
 	defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
... ...
@@ -1454,3 +1454,29 @@ func TestCopyVolumeContent(t *testing.T) {
1454 1454
 		t.Fatal("Container failed to transfer content to volume")
1455 1455
 	}
1456 1456
 }
1457
+
1458
+func TestRunCleanupCmdOnEntrypoint(t *testing.T) {
1459
+	name := "testrunmdcleanuponentrypoint"
1460
+	defer deleteImages(name)
1461
+	defer deleteAllContainers()
1462
+	if _, err := buildImage(name,
1463
+		`FROM busybox
1464
+		ENTRYPOINT ["echo"]
1465
+        CMD ["testingpoint"]`,
1466
+		true); err != nil {
1467
+		t.Fatal(err)
1468
+	}
1469
+	runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name)
1470
+	out, exit, err := runCommandWithOutput(runCmd)
1471
+	if err != nil {
1472
+		t.Fatalf("Error: %v, out: %q", err, out)
1473
+	}
1474
+	if exit != 0 {
1475
+		t.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
1476
+	}
1477
+	out = strings.TrimSpace(out)
1478
+	if out != "root" {
1479
+		t.Fatalf("Expected output root, got %q", out)
1480
+	}
1481
+	logDone("run - cleanup cmd on --entrypoint")
1482
+}
... ...
@@ -84,10 +84,10 @@ func Merge(userConf, imageConf *Config) error {
84 84
 		}
85 85
 	}
86 86
 
87
-	if len(userConf.Cmd) == 0 {
88
-		userConf.Cmd = imageConf.Cmd
89
-	}
90 87
 	if len(userConf.Entrypoint) == 0 {
88
+		if len(userConf.Cmd) == 0 {
89
+			userConf.Cmd = imageConf.Cmd
90
+		}
91 91
 		userConf.Entrypoint = imageConf.Entrypoint
92 92
 	}
93 93
 	if userConf.WorkingDir == "" {