Forráskód Böngészése

Add show error when attach to a paused container

Signed-off-by: Lei Jitang <leijitang@huawei.com>
Lei Jitang 9 éve
szülő
commit
de1d611990

+ 4 - 0
api/client/attach.go

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

+ 4 - 0
api/server/router/local/container.go

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

+ 6 - 0
daemon/daemon.go

@@ -163,6 +163,12 @@ func (daemon *Daemon) Exists(id string) bool {
 	return c != nil
 	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 {
 func (daemon *Daemon) containerRoot(id string) string {
 	return filepath.Join(daemon.repository, id)
 	return filepath.Join(daemon.repository, id)
 }
 }

+ 8 - 0
errors/daemon.go

@@ -46,6 +46,14 @@ var (
 		HTTPStatusCode: http.StatusInternalServerError,
 		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
 	// ErrorCodeAlreadyPaused is generated when we attempt to pause a
 	// container when its already paused.
 	// container when its already paused.
 	ErrorCodeAlreadyPaused = errcode.Register(errGroup, errcode.ErrorDescriptor{
 	ErrorCodeAlreadyPaused = errcode.Register(errGroup, errcode.ErrorDescriptor{

+ 10 - 0
integration-cli/docker_cli_attach_test.go

@@ -9,6 +9,7 @@ import (
 	"sync"
 	"sync"
 	"time"
 	"time"
 
 
+	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/go-check/check"
 	"github.com/go-check/check"
 )
 )
 
 
@@ -150,3 +151,12 @@ func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
 	c.Assert(err, check.IsNil)
 	c.Assert(err, check.IsNil)
 	c.Assert(running, check.Equals, "true")
 	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")
+}