From c92377e300d2e9863ffa8eda9c3166a039b60e09 Mon Sep 17 00:00:00 2001 From: Regan McCooey Date: Tue, 14 Apr 2015 00:17:07 +0900 Subject: [PATCH] docker kill should return error if container is not running. Assuming that docker kill is trying to actually kill the container is a mistake. If the container is not running we should report it back to the caller as a error. Docker-DCO-1.1-Signed-off-by: Dan Walsh (github: rhatdan) Docker-DCO-1.1-Signed-off-by: Regan McCooey (github: rmccooey27) Docker-DCO-1.1-Signed-off-by: Regan McCooey (github: rhatdan) --- api/server/server.go | 2 +- daemon/container.go | 2 +- integration-cli/docker_cli_kill_test.go | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/api/server/server.go b/api/server/server.go index cdc6c18154..2e7ffbf95e 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -286,7 +286,7 @@ func (s *Server) postContainersKill(eng *engine.Engine, version version.Version, name := vars["name"] // If we have a signal, look at it. Otherwise, do nothing - if sigStr := vars["signal"]; sigStr != "" { + if sigStr := r.Form.Get("signal"); sigStr != "" { // Check if we passed the signal as a number: // The largest legal signal is 31, so let's parse on 5 bits sig, err = strconv.ParseUint(sigStr, 10, 5) diff --git a/daemon/container.go b/daemon/container.go index bdfcbf4477..8a9f87d215 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -710,7 +710,7 @@ func (container *Container) KillSig(sig int) error { } if !container.Running { - return nil + return fmt.Errorf("Container %s is not running", container.ID) } // signal to the monitor that it should not restart the container diff --git a/integration-cli/docker_cli_kill_test.go b/integration-cli/docker_cli_kill_test.go index d08709671d..aa11a782b5 100644 --- a/integration-cli/docker_cli_kill_test.go +++ b/integration-cli/docker_cli_kill_test.go @@ -40,6 +40,28 @@ func (s *DockerSuite) TestKillContainer(c *check.C) { } +func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) { + runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top") + out, _, err := runCommandWithOutput(runCmd) + if err != nil { + c.Fatal(out, err) + } + + cleanedContainerID := strings.TrimSpace(out) + + stopCmd := exec.Command(dockerBinary, "stop", cleanedContainerID) + if out, _, err = runCommandWithOutput(stopCmd); err != nil { + c.Fatalf("failed to stop container: %s, %v", out, err) + } + + killCmd := exec.Command(dockerBinary, "kill", "-s", "30", cleanedContainerID) + if _, _, err = runCommandWithOutput(killCmd); err == nil { + c.Fatalf("kill succeeded on a stopped container") + } + + deleteContainer(cleanedContainerID) +} + func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) { runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "top") out, _, err := runCommandWithOutput(runCmd)