2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdAttach attaches to a running container.
|
|
|
|
//
|
|
|
|
// Usage: docker attach [OPTIONS] CONTAINER
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdAttach(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("attach", []string{"CONTAINER"}, Cli.DockerCommands["attach"].Description, true)
|
2015-11-09 09:37:24 -05: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 17:03:39 -05:00
|
|
|
detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
|
2015-05-05 00:18:28 -04:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
cmd.Require(flag.Exact, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
c, err := cli.client.ContainerInspect(cmd.Arg(0))
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
if !c.State.Running {
|
2015-03-24 23:57:23 -04:00
|
|
|
return fmt.Errorf("You cannot attach to a stopped container, start it first")
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:00:09 -04:00
|
|
|
if c.State.Paused {
|
|
|
|
return fmt.Errorf("You cannot attach to a paused container, unpause it first")
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
if err := cli.CheckTtyInput(!*noStdin, c.Config.Tty); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-03 17:03:39 -05:00
|
|
|
if *detachKeys != "" {
|
|
|
|
cli.configFile.DetachKeys = *detachKeys
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
options := types.ContainerAttachOptions{
|
|
|
|
ContainerID: cmd.Arg(0),
|
|
|
|
Stream: true,
|
|
|
|
Stdin: !*noStdin && c.Config.OpenStdin,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
2016-01-03 17:03:39 -05:00
|
|
|
DetachKeys: cli.configFile.DetachKeys,
|
2015-12-05 22:22:00 -05:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
var in io.ReadCloser
|
|
|
|
if options.Stdin {
|
2015-03-24 23:57:23 -04:00
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
if *proxy && !c.Config.Tty {
|
2015-12-05 22:22:00 -05:00
|
|
|
sigc := cli.forwardAllSignals(options.ContainerID)
|
2015-03-24 23:57:23 -04:00
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
resp, err := cli.client.ContainerAttach(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
2016-01-20 16:18:36 -05:00
|
|
|
if in != nil && c.Config.Tty {
|
|
|
|
if err := cli.setRawTerminal(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cli.restoreTerminal(in)
|
|
|
|
}
|
2015-12-05 22:22:00 -05:00
|
|
|
|
2016-01-22 06:35:55 -05:00
|
|
|
if c.Config.Tty && cli.isTerminalOut {
|
|
|
|
height, width := cli.getTtySize()
|
|
|
|
// To handle the case where a user repeatedly attaches/detaches without resizing their
|
2016-02-11 18:21:52 -05:00
|
|
|
// terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
|
2016-01-22 06:35:55 -05:00
|
|
|
// resize it, then go back to normal. Without this, every attach after the first will
|
|
|
|
// require the user to manually resize or hit enter.
|
|
|
|
cli.resizeTtyTo(cmd.Arg(0), height+1, width+1, false)
|
|
|
|
|
|
|
|
// After the above resizing occurs, the call to monitorTtySize below will handle resetting back
|
|
|
|
// to the actual size.
|
|
|
|
if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
|
|
|
|
logrus.Debugf("Error monitoring TTY size: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
if err := cli.holdHijackedConnection(c.Config.Tty, in, cli.out, cli.err, resp); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:22:00 -05:00
|
|
|
_, status, err := getExitCode(cli, options.ContainerID)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != 0 {
|
2015-05-05 00:18:28 -04:00
|
|
|
return Cli.StatusError{StatusCode: status}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|