From 6e86733b47faf0d7629751987346022544b65cb7 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sat, 18 Jun 2016 16:43:30 -0700 Subject: [PATCH] Fix `docker start` error with renamed container This fix tries to fix the issue raised in #23716 where `docker start` causes an error of `No such container:` if the container has been renamed before `docker start` returns. The issue is that `docker start` use container name passed at the beginning to check for exit code at the end of the `docker start`. This fix addresses the issue by always use container's `ID` to get the information during `docker start`. Additional integration tests have been added to cover this fix. This fix fixes #23716. Signed-off-by: Yong Tang --- api/client/container/start.go | 11 ++++++----- integration-cli/docker_cli_start_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/api/client/container/start.go b/api/client/container/start.go index ca074e68e0..df955d2816 100644 --- a/api/client/container/start.go +++ b/api/client/container/start.go @@ -63,8 +63,9 @@ func runStart(dockerCli *client.DockerCli, opts *startOptions) error { return err } + // We always use c.ID instead of container to maintain consistency during `docker start` if !c.Config.Tty { - sigc := dockerCli.ForwardAllSignals(ctx, container) + sigc := dockerCli.ForwardAllSignals(ctx, c.ID) defer signal.StopCatch(sigc) } @@ -86,7 +87,7 @@ func runStart(dockerCli *client.DockerCli, opts *startOptions) error { in = dockerCli.In() } - resp, errAttach := dockerCli.Client().ContainerAttach(ctx, container, options) + resp, errAttach := dockerCli.Client().ContainerAttach(ctx, c.ID, options) if errAttach != nil && errAttach != httputil.ErrPersistEOF { // ContainerAttach return an ErrPersistEOF (connection closed) // means server met an error and put it in Hijacked connection @@ -103,7 +104,7 @@ func runStart(dockerCli *client.DockerCli, opts *startOptions) error { }) // 3. Start the container. - if err := dockerCli.Client().ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil { + if err := dockerCli.Client().ContainerStart(ctx, c.ID, types.ContainerStartOptions{}); err != nil { cancelFun() <-cErr return err @@ -111,14 +112,14 @@ func runStart(dockerCli *client.DockerCli, opts *startOptions) error { // 4. Wait for attachment to break. if c.Config.Tty && dockerCli.IsTerminalOut() { - if err := dockerCli.MonitorTtySize(ctx, container, false); err != nil { + if err := dockerCli.MonitorTtySize(ctx, c.ID, false); err != nil { fmt.Fprintf(dockerCli.Err(), "Error monitoring TTY size: %s\n", err) } } if attchErr := <-cErr; attchErr != nil { return attchErr } - _, status, err := getExitCode(dockerCli, ctx, container) + _, status, err := getExitCode(dockerCli, ctx, c.ID) if err != nil { return err } diff --git a/integration-cli/docker_cli_start_test.go b/integration-cli/docker_cli_start_test.go index 2c984fabe9..1c6a3cf2ae 100644 --- a/integration-cli/docker_cli_start_test.go +++ b/integration-cli/docker_cli_start_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os/exec" "strings" "time" @@ -171,3 +172,16 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) { c.Assert(out, checker.Equals, expected) } } + +// Test case for #23716 +func (s *DockerSuite) TestStartAttachWithRename(c *check.C) { + testRequires(c, DaemonIsLinux) + dockerCmd(c, "create", "-t", "--name", "before", "busybox") + go func() { + c.Assert(waitRun("before"), checker.IsNil) + dockerCmd(c, "rename", "before", "after") + dockerCmd(c, "stop", "--time=2", "after") + }() + _, stderr, _, _ := runCommandWithStdoutStderr(exec.Command(dockerBinary, "start", "-a", "before")) + c.Assert(stderr, checker.Not(checker.Contains), "No such container") +}