github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/container/attach.go (about) 1 package container 2 3 import ( 4 "errors" 5 "io" 6 "net/http/httputil" 7 8 "github.com/Sirupsen/logrus" 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/cli" 11 "github.com/docker/docker/cli/command" 12 "github.com/docker/docker/pkg/signal" 13 "github.com/spf13/cobra" 14 "golang.org/x/net/context" 15 ) 16 17 type attachOptions struct { 18 noStdin bool 19 proxy bool 20 detachKeys string 21 22 container string 23 } 24 25 // NewAttachCommand creates a new cobra.Command for `docker attach` 26 func NewAttachCommand(dockerCli *command.DockerCli) *cobra.Command { 27 var opts attachOptions 28 29 cmd := &cobra.Command{ 30 Use: "attach [OPTIONS] CONTAINER", 31 Short: "Attach to a running container", 32 Args: cli.ExactArgs(1), 33 RunE: func(cmd *cobra.Command, args []string) error { 34 opts.container = args[0] 35 return runAttach(dockerCli, &opts) 36 }, 37 } 38 39 flags := cmd.Flags() 40 flags.BoolVar(&opts.noStdin, "no-stdin", false, "Do not attach STDIN") 41 flags.BoolVar(&opts.proxy, "sig-proxy", true, "Proxy all received signals to the process") 42 flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container") 43 return cmd 44 } 45 46 func runAttach(dockerCli *command.DockerCli, opts *attachOptions) error { 47 ctx := context.Background() 48 client := dockerCli.Client() 49 50 c, err := client.ContainerInspect(ctx, opts.container) 51 if err != nil { 52 return err 53 } 54 55 if !c.State.Running { 56 return errors.New("You cannot attach to a stopped container, start it first") 57 } 58 59 if c.State.Paused { 60 return errors.New("You cannot attach to a paused container, unpause it first") 61 } 62 63 if err := dockerCli.In().CheckTty(!opts.noStdin, c.Config.Tty); err != nil { 64 return err 65 } 66 67 if opts.detachKeys != "" { 68 dockerCli.ConfigFile().DetachKeys = opts.detachKeys 69 } 70 71 options := types.ContainerAttachOptions{ 72 Stream: true, 73 Stdin: !opts.noStdin && c.Config.OpenStdin, 74 Stdout: true, 75 Stderr: true, 76 DetachKeys: dockerCli.ConfigFile().DetachKeys, 77 } 78 79 var in io.ReadCloser 80 if options.Stdin { 81 in = dockerCli.In() 82 } 83 84 if opts.proxy && !c.Config.Tty { 85 sigc := ForwardAllSignals(ctx, dockerCli, opts.container) 86 defer signal.StopCatch(sigc) 87 } 88 89 resp, errAttach := client.ContainerAttach(ctx, opts.container, options) 90 if errAttach != nil && errAttach != httputil.ErrPersistEOF { 91 // ContainerAttach returns an ErrPersistEOF (connection closed) 92 // means server met an error and put it in Hijacked connection 93 // keep the error and read detailed error message from hijacked connection later 94 return errAttach 95 } 96 defer resp.Close() 97 98 if c.Config.Tty && dockerCli.Out().IsTerminal() { 99 height, width := dockerCli.Out().GetTtySize() 100 // To handle the case where a user repeatedly attaches/detaches without resizing their 101 // terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially 102 // resize it, then go back to normal. Without this, every attach after the first will 103 // require the user to manually resize or hit enter. 104 resizeTtyTo(ctx, client, opts.container, height+1, width+1, false) 105 106 // After the above resizing occurs, the call to MonitorTtySize below will handle resetting back 107 // to the actual size. 108 if err := MonitorTtySize(ctx, dockerCli, opts.container, false); err != nil { 109 logrus.Debugf("Error monitoring TTY size: %s", err) 110 } 111 } 112 if err := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil { 113 return err 114 } 115 116 if errAttach != nil { 117 return errAttach 118 } 119 120 _, status, err := getExitCode(ctx, dockerCli, opts.container) 121 if err != nil { 122 return err 123 } 124 if status != 0 { 125 return cli.StatusError{StatusCode: status} 126 } 127 128 return nil 129 }