From 69a5b827dcf01a6de5949a161606058017014cdc Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Wed, 5 Nov 2014 18:23:42 -0800 Subject: [PATCH] See #8379 - if the container doesn't start I added code to make sure that if no other processing sets the container.exitCode to a non-zero value when we make sure its done before we return. I also made sure that while trying to start the CMD/ENTRYPOINT, if it fails, then we set the container.exitCode to the exitStatus from the exec(). Closes #8379 Signed-off-by: Doug Davis --- daemon/container.go | 4 ++++ daemon/monitor.go | 3 +++ integration-cli/docker_cli_run_test.go | 30 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/daemon/container.go b/daemon/container.go index 8525206e4a..905dbd5707 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -302,6 +302,10 @@ func (container *Container) Start() (err error) { defer func() { if err != nil { container.setError(err) + // if no one else has set it, make sure we don't leave it at zero + if container.ExitCode == 0 { + container.ExitCode = 128 + } container.toDisk() container.cleanup() } diff --git a/daemon/monitor.go b/daemon/monitor.go index cbb74c335b..d0d9d70a99 100644 --- a/daemon/monitor.go +++ b/daemon/monitor.go @@ -138,6 +138,7 @@ func (m *containerMonitor) Start() error { // if we receive an internal error from the initial start of a container then lets // return it instead of entering the restart loop if m.container.RestartCount == 0 { + m.container.ExitCode = exitStatus m.resetContainer(false) return err @@ -163,10 +164,12 @@ func (m *containerMonitor) Start() error { // we need to check this before reentering the loop because the waitForNextRestart could have // been terminated by a request from a user if m.shouldStop { + m.container.ExitCode = exitStatus return err } continue } + m.container.ExitCode = exitStatus m.container.LogEvent("die") m.resetContainer(true) return err diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 4c3e8d0a08..d536c626bb 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -2538,3 +2538,33 @@ func TestRunAllowPortRangeThroughExpose(t *testing.T) { } logDone("run - allow port range through --expose flag") } + +func TestRunUnknownCommand(t *testing.T) { + defer deleteAllContainers() + runCmd := exec.Command(dockerBinary, "create", "busybox", "/bin/nada") + cID, _, _, err := runCommandWithStdoutStderr(runCmd) + if err != nil { + t.Fatalf("Failed to create container: %v, output: %q", err, cID) + } + cID = strings.TrimSpace(cID) + + runCmd = exec.Command(dockerBinary, "start", cID) + _, _, _, err = runCommandWithStdoutStderr(runCmd) + if err == nil { + t.Fatalf("Container should not have been able to start!") + } + + runCmd = exec.Command(dockerBinary, "inspect", "--format={{.State.ExitCode}}", cID) + rc, _, _, err2 := runCommandWithStdoutStderr(runCmd) + rc = strings.TrimSpace(rc) + + if err2 != nil { + t.Fatalf("Error getting status of container: %v", err2) + } + + if rc != "-1" { + t.Fatalf("ExitCode(%v) was supposed to be -1", rc) + } + + logDone("run - Unknown Command") +}