2016-06-05 12:17:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http/httputil"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-05 12:17:39 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-05 12:17:39 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-06-05 12:17:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-12-24 19:05:37 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-06-05 12:17:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type attachOptions struct {
|
|
|
|
noStdin bool
|
|
|
|
proxy bool
|
|
|
|
detachKeys string
|
|
|
|
|
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:55:18 -04:00
|
|
|
// NewAttachCommand creates a new cobra.Command for `docker attach`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewAttachCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-05 12:17:39 -04:00
|
|
|
var opts attachOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "attach [OPTIONS] CONTAINER",
|
2017-03-31 16:22:21 -04:00
|
|
|
Short: "Attach local standard input, output, and error streams to a running container",
|
2016-06-05 12:17:39 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
return runAttach(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVar(&opts.noStdin, "no-stdin", false, "Do not attach STDIN")
|
|
|
|
flags.BoolVar(&opts.proxy, "sig-proxy", true, "Proxy all received signals to the process")
|
|
|
|
flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runAttach(dockerCli *command.DockerCli, opts *attachOptions) error {
|
2016-06-05 12:17:39 -04:00
|
|
|
ctx := context.Background()
|
2016-08-29 12:23:15 -04:00
|
|
|
client := dockerCli.Client()
|
2016-06-05 12:17:39 -04:00
|
|
|
|
2016-08-29 12:23:15 -04:00
|
|
|
c, err := client.ContainerInspect(ctx, opts.container)
|
2016-06-05 12:17:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.State.Running {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New("You cannot attach to a stopped container, start it first")
|
2016-06-05 12:17:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.State.Paused {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New("You cannot attach to a paused container, unpause it first")
|
2016-06-05 12:17:39 -04:00
|
|
|
}
|
|
|
|
|
2016-08-29 11:52:05 -04:00
|
|
|
if err := dockerCli.In().CheckTty(!opts.noStdin, c.Config.Tty); err != nil {
|
2016-06-05 12:17:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.detachKeys != "" {
|
|
|
|
dockerCli.ConfigFile().DetachKeys = opts.detachKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.ContainerAttachOptions{
|
|
|
|
Stream: true,
|
|
|
|
Stdin: !opts.noStdin && c.Config.OpenStdin,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
DetachKeys: dockerCli.ConfigFile().DetachKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
var in io.ReadCloser
|
|
|
|
if options.Stdin {
|
|
|
|
in = dockerCli.In()
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.proxy && !c.Config.Tty {
|
2016-08-29 12:23:15 -04:00
|
|
|
sigc := ForwardAllSignals(ctx, dockerCli, opts.container)
|
2016-06-05 12:17:39 -04:00
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
|
2016-08-29 12:23:15 -04:00
|
|
|
resp, errAttach := client.ContainerAttach(ctx, opts.container, options)
|
2016-06-05 12:17:39 -04:00
|
|
|
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
|
|
|
// ContainerAttach returns an ErrPersistEOF (connection closed)
|
|
|
|
// means server met an error and put it in Hijacked connection
|
|
|
|
// keep the error and read detailed error message from hijacked connection later
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
|
|
|
|
2016-08-29 11:11:29 -04:00
|
|
|
if c.Config.Tty && dockerCli.Out().IsTerminal() {
|
|
|
|
height, width := dockerCli.Out().GetTtySize()
|
2016-06-05 12:17:39 -04:00
|
|
|
// To handle the case where a user repeatedly attaches/detaches without resizing their
|
|
|
|
// terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
|
|
|
|
// resize it, then go back to normal. Without this, every attach after the first will
|
|
|
|
// require the user to manually resize or hit enter.
|
2016-08-29 12:23:15 -04:00
|
|
|
resizeTtyTo(ctx, client, opts.container, height+1, width+1, false)
|
2016-06-05 12:17:39 -04:00
|
|
|
|
|
|
|
// After the above resizing occurs, the call to MonitorTtySize below will handle resetting back
|
|
|
|
// to the actual size.
|
2016-08-29 12:23:15 -04:00
|
|
|
if err := MonitorTtySize(ctx, dockerCli, opts.container, false); err != nil {
|
2016-06-05 12:17:39 -04:00
|
|
|
logrus.Debugf("Error monitoring TTY size: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 13:36:29 -04:00
|
|
|
if err := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil {
|
2016-06-05 12:17:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if errAttach != nil {
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
|
2016-11-12 01:14:34 -05:00
|
|
|
_, status, err := getExitCode(ctx, dockerCli, opts.container)
|
2016-06-05 12:17:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != 0 {
|
|
|
|
return cli.StatusError{StatusCode: status}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|