2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdAttach attaches to a running container.
|
|
|
|
//
|
|
|
|
// Usage: docker attach [OPTIONS] CONTAINER
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdAttach(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("attach", []string{"CONTAINER"}, Cli.DockerCommands["attach"].Description, true)
|
2015-11-09 14:37:24 +00:00
|
|
|
noStdin := cmd.Bool([]string{"-no-stdin"}, false, "Do not attach STDIN")
|
|
|
|
proxy := cmd.Bool([]string{"-sig-proxy"}, true, "Proxy all received signals to the process")
|
2016-01-03 22:03:39 +00:00
|
|
|
detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
|
2015-05-05 04:18:28 +00:00
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
cmd.Require(flag.Exact, 1)
|
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-12-06 03:22:00 +00:00
|
|
|
c, err := cli.client.ContainerInspect(cmd.Arg(0))
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:17:14 +00:00
|
|
|
if !c.State.Running {
|
2015-03-25 03:57:23 +00:00
|
|
|
return fmt.Errorf("You cannot attach to a stopped container, start it first")
|
|
|
|
}
|
|
|
|
|
2015-10-29 01:00:09 +00:00
|
|
|
if c.State.Paused {
|
|
|
|
return fmt.Errorf("You cannot attach to a paused container, unpause it first")
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:17:14 +00:00
|
|
|
if err := cli.CheckTtyInput(!*noStdin, c.Config.Tty); err != nil {
|
2015-03-25 03:57:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:17:14 +00:00
|
|
|
if c.Config.Tty && cli.isTerminalOut {
|
2015-03-25 03:57:23 +00:00
|
|
|
if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error monitoring TTY size: %s", err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 22:03:39 +00:00
|
|
|
if *detachKeys != "" {
|
|
|
|
cli.configFile.DetachKeys = *detachKeys
|
|
|
|
}
|
|
|
|
|
2015-12-06 03:22:00 +00:00
|
|
|
options := types.ContainerAttachOptions{
|
|
|
|
ContainerID: cmd.Arg(0),
|
|
|
|
Stream: true,
|
|
|
|
Stdin: !*noStdin && c.Config.OpenStdin,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
2016-01-03 22:03:39 +00:00
|
|
|
DetachKeys: cli.configFile.DetachKeys,
|
2015-12-06 03:22:00 +00:00
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-12-06 03:22:00 +00:00
|
|
|
var in io.ReadCloser
|
|
|
|
if options.Stdin {
|
2015-03-25 03:57:23 +00:00
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:17:14 +00:00
|
|
|
if *proxy && !c.Config.Tty {
|
2015-12-06 03:22:00 +00:00
|
|
|
sigc := cli.forwardAllSignals(options.ContainerID)
|
2015-03-25 03:57:23 +00:00
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
|
2015-12-06 03:22:00 +00:00
|
|
|
resp, err := cli.client.ContainerAttach(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
2016-01-20 21:18:36 +00:00
|
|
|
if in != nil && c.Config.Tty {
|
|
|
|
if err := cli.setRawTerminal(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cli.restoreTerminal(in)
|
|
|
|
}
|
2015-12-06 03:22:00 +00:00
|
|
|
|
|
|
|
if err := cli.holdHijackedConnection(c.Config.Tty, in, cli.out, cli.err, resp); err != nil {
|
2015-03-25 03:57:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-06 03:22:00 +00:00
|
|
|
_, status, err := getExitCode(cli, options.ContainerID)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != 0 {
|
2015-05-05 04:18:28 +00:00
|
|
|
return Cli.StatusError{StatusCode: status}
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|