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 <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-06-18 16:43:30 -07:00
parent 83f2feb72d
commit 6e86733b47
2 changed files with 20 additions and 5 deletions

View file

@ -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
}

View file

@ -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")
}