Merge pull request #16579 from coolljt0725/fix_attach_paused_container

Add show error when attach to a paused container
This commit is contained in:
Arnaud Porterie 2015-10-31 07:43:22 -07:00
commit 5719d01066
5 changed files with 32 additions and 0 deletions

View File

@ -41,6 +41,10 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
return fmt.Errorf("You cannot attach to a stopped container, start it first")
}
if c.State.Paused {
return fmt.Errorf("You cannot attach to a paused container, unpause it first")
}
if err := cli.CheckTtyInput(!*noStdin, c.Config.Tty); err != nil {
return err
}

View File

@ -396,6 +396,10 @@ func (s *router) postContainersAttach(ctx context.Context, w http.ResponseWriter
return derr.ErrorCodeNoSuchContainer.WithArgs(containerName)
}
if s.daemon.IsPaused(containerName) {
return derr.ErrorCodePausedContainer.WithArgs(containerName)
}
inStream, outStream, err := httputils.HijackConnection(w)
if err != nil {
return err

View File

@ -164,6 +164,12 @@ func (daemon *Daemon) Exists(id string) bool {
return c != nil
}
// IsPaused returns a bool indicating if the specified container is paused.
func (daemon *Daemon) IsPaused(id string) bool {
c, _ := daemon.Get(id)
return c.State.isPaused()
}
func (daemon *Daemon) containerRoot(id string) string {
return filepath.Join(daemon.repository, id)
}

View File

@ -46,6 +46,14 @@ var (
HTTPStatusCode: http.StatusInternalServerError,
})
// ErrorCodePausedContainer is generated when we attempt to attach a
// container but its paused.
ErrorCodePausedContainer = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "CONTAINERPAUSED",
Message: "Container %s is paused. Unpause the container before attach",
Description: "The specified container is paused, unpause the container before attach",
HTTPStatusCode: http.StatusConflict,
})
// ErrorCodeAlreadyPaused is generated when we attempt to pause a
// container when its already paused.
ErrorCodeAlreadyPaused = errcode.Register(errGroup, errcode.ErrorDescriptor{

View File

@ -9,6 +9,7 @@ import (
"sync"
"time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
@ -150,3 +151,12 @@ func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(running, check.Equals, "true")
}
func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
defer unpauseAllContainers()
dockerCmd(c, "run", "-d", "--name=test", "busybox", "top")
dockerCmd(c, "pause", "test")
out, _, err := dockerCmdWithError("attach", "test")
c.Assert(err, checker.NotNil, check.Commentf(out))
c.Assert(out, checker.Contains, "You cannot attach to a paused container, unpause it first")
}