Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit aa2d6db

Browse files
committed
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)
1 parent 1b6546b commit aa2d6db

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

builder/builder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type buildFile struct {
7171
outOld io.Writer
7272
sf *utils.StreamFormatter
7373

74-
// cmdSet indicates is CMD was setted in current Dockerfile
74+
// cmdSet indicates is CMD was set in current Dockerfile
7575
cmdSet bool
7676
}
7777

@@ -202,7 +202,8 @@ func (b *buildFile) CmdRun(args string) error {
202202
}
203203

204204
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
206207
runconfig.Merge(b.config, config)
207208

208209
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)

integration-cli/docker_cli_run_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,3 +1454,29 @@ func TestCopyVolumeContent(t *testing.T) {
14541454
t.Fatal("Container failed to transfer content to volume")
14551455
}
14561456
}
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+
}

runconfig/merge.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ func Merge(userConf, imageConf *Config) error {
8484
}
8585
}
8686

87-
if len(userConf.Cmd) == 0 {
88-
userConf.Cmd = imageConf.Cmd
89-
}
9087
if len(userConf.Entrypoint) == 0 {
88+
if len(userConf.Cmd) == 0 {
89+
userConf.Cmd = imageConf.Cmd
90+
}
9191
userConf.Entrypoint = imageConf.Entrypoint
9292
}
9393
if userConf.WorkingDir == "" {

0 commit comments

Comments
 (0)