github.com/rentongzhang/docker@v1.8.2-rc1/api/client/attach.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "net/url" 8 9 "github.com/Sirupsen/logrus" 10 "github.com/docker/docker/api/types" 11 Cli "github.com/docker/docker/cli" 12 flag "github.com/docker/docker/pkg/mflag" 13 "github.com/docker/docker/pkg/signal" 14 ) 15 16 // CmdAttach attaches to a running container. 17 // 18 // Usage: docker attach [OPTIONS] CONTAINER 19 func (cli *DockerCli) CmdAttach(args ...string) error { 20 cmd := Cli.Subcmd("attach", []string{"CONTAINER"}, "Attach to a running container", true) 21 noStdin := cmd.Bool([]string{"#nostdin", "-no-stdin"}, false, "Do not attach STDIN") 22 proxy := cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy all received signals to the process") 23 24 cmd.Require(flag.Exact, 1) 25 26 cmd.ParseFlags(args, true) 27 28 serverResp, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil, nil) 29 if err != nil { 30 return err 31 } 32 33 defer serverResp.body.Close() 34 35 var c types.ContainerJSON 36 if err := json.NewDecoder(serverResp.body).Decode(&c); err != nil { 37 return err 38 } 39 40 if !c.State.Running { 41 return fmt.Errorf("You cannot attach to a stopped container, start it first") 42 } 43 44 if err := cli.CheckTtyInput(!*noStdin, c.Config.Tty); err != nil { 45 return err 46 } 47 48 if c.Config.Tty && cli.isTerminalOut { 49 if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil { 50 logrus.Debugf("Error monitoring TTY size: %s", err) 51 } 52 } 53 54 var in io.ReadCloser 55 56 v := url.Values{} 57 v.Set("stream", "1") 58 if !*noStdin && c.Config.OpenStdin { 59 v.Set("stdin", "1") 60 in = cli.in 61 } 62 63 v.Set("stdout", "1") 64 v.Set("stderr", "1") 65 66 if *proxy && !c.Config.Tty { 67 sigc := cli.forwardAllSignals(cmd.Arg(0)) 68 defer signal.StopCatch(sigc) 69 } 70 71 if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), c.Config.Tty, in, cli.out, cli.err, nil, nil); err != nil { 72 return err 73 } 74 75 _, status, err := getExitCode(cli, cmd.Arg(0)) 76 if err != nil { 77 return err 78 } 79 if status != 0 { 80 return Cli.StatusError{StatusCode: status} 81 } 82 83 return nil 84 }