2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-13 10:17:14 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-13 10:17:14 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
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"
|
|
|
|
)
|
|
|
|
|
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-07-03 05:26:09 -04:00
|
|
|
noStdin := cmd.Bool([]string{"#nostdin", "-no-stdin"}, false, "Do not attach STDIN")
|
|
|
|
proxy := cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy all received signals to the process")
|
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-07-09 22:05:50 -04:00
|
|
|
serverResp, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
var c types.ContainerJSON
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&c); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
if c.Config.Tty && cli.isTerminalOut {
|
2015-03-24 23:57:23 -04:00
|
|
|
if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error monitoring TTY size: %s", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var in io.ReadCloser
|
|
|
|
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("stream", "1")
|
2015-04-13 10:17:14 -04:00
|
|
|
if !*noStdin && c.Config.OpenStdin {
|
2015-03-24 23:57:23 -04:00
|
|
|
v.Set("stdin", "1")
|
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Set("stdout", "1")
|
|
|
|
v.Set("stderr", "1")
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
if *proxy && !c.Config.Tty {
|
2015-03-24 23:57:23 -04:00
|
|
|
sigc := cli.forwardAllSignals(cmd.Arg(0))
|
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:17:14 -04:00
|
|
|
if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), c.Config.Tty, in, cli.out, cli.err, nil, nil); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, status, err := getExitCode(cli, cmd.Arg(0))
|
|
|
|
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
|
|
|
|
}
|