2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-03-23 11:34:47 +00:00
|
|
|
"net/http/httputil"
|
2015-03-25 03:57:23 +00:00
|
|
|
"os"
|
2015-12-06 03:53:52 +00:00
|
|
|
"strings"
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2016-03-16 19:19:22 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (cli *DockerCli) forwardAllSignals(cid string) chan os.Signal {
|
|
|
|
sigc := make(chan os.Signal, 128)
|
|
|
|
signal.CatchAll(sigc)
|
|
|
|
go func() {
|
|
|
|
for s := range sigc {
|
2016-03-18 20:50:18 +00:00
|
|
|
if s == signal.SIGCHLD || s == signal.SIGPIPE {
|
2015-03-25 03:57:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var sig string
|
|
|
|
for sigStr, sigN := range signal.SignalMap {
|
|
|
|
if sigN == s {
|
|
|
|
sig = sigStr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sig == "" {
|
2015-05-09 18:33:06 +00:00
|
|
|
fmt.Fprintf(cli.err, "Unsupported signal: %v. Discarding.\n", s)
|
2015-09-24 09:21:20 +00:00
|
|
|
continue
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-12-03 23:40:56 +00:00
|
|
|
|
2016-03-16 19:19:22 +00:00
|
|
|
if err := cli.client.ContainerKill(context.Background(), cid, sig); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error sending signal: %s", err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return sigc
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:56:41 +00:00
|
|
|
// CmdStart starts one or more containers.
|
2015-03-25 17:34:41 +00:00
|
|
|
//
|
|
|
|
// Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdStart(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("start", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["start"].Description, true)
|
2015-07-03 09:26:09 +00:00
|
|
|
attach := cmd.Bool([]string{"a", "-attach"}, false, "Attach STDOUT/STDERR and forward signals")
|
|
|
|
openStdin := cmd.Bool([]string{"i", "-interactive"}, false, "Attach container's STDIN")
|
2016-01-03 22:03:39 +00:00
|
|
|
detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
|
2015-07-03 09:26:09 +00:00
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
|
|
|
cmd.ParseFlags(args, true)
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
if *attach || *openStdin {
|
2015-12-06 03:53:52 +00:00
|
|
|
// We're going to attach to a container.
|
|
|
|
// 1. Ensure we only have one container.
|
2015-03-25 03:57:23 +00:00
|
|
|
if cmd.NArg() > 1 {
|
|
|
|
return fmt.Errorf("You cannot start and attach multiple containers at once.")
|
|
|
|
}
|
|
|
|
|
2015-12-06 03:53:52 +00:00
|
|
|
// 2. Attach to the container.
|
2016-04-13 08:33:46 +00:00
|
|
|
container := cmd.Arg(0)
|
|
|
|
c, err := cli.client.ContainerInspect(context.Background(), container)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-06 03:53:52 +00:00
|
|
|
if !c.Config.Tty {
|
2016-04-13 08:33:46 +00:00
|
|
|
sigc := cli.forwardAllSignals(container)
|
2015-12-06 03:53:52 +00:00
|
|
|
defer signal.StopCatch(sigc)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-04-27 21:11:29 +00:00
|
|
|
|
2016-01-03 22:03:39 +00:00
|
|
|
if *detachKeys != "" {
|
|
|
|
cli.configFile.DetachKeys = *detachKeys
|
|
|
|
}
|
|
|
|
|
2015-12-06 03:53:52 +00:00
|
|
|
options := types.ContainerAttachOptions{
|
2016-04-13 08:33:46 +00:00
|
|
|
Stream: true,
|
|
|
|
Stdin: *openStdin && c.Config.OpenStdin,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
DetachKeys: cli.configFile.DetachKeys,
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var in io.ReadCloser
|
2016-03-25 01:25:50 +00:00
|
|
|
|
2015-12-06 03:53:52 +00:00
|
|
|
if options.Stdin {
|
2015-03-25 03:57:23 +00:00
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
resp, errAttach := cli.client.ContainerAttach(context.Background(), container, options)
|
2016-03-23 11:34:47 +00:00
|
|
|
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
|
|
|
// ContainerAttach return 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
|
|
|
|
return errAttach
|
2015-12-06 03:53:52 +00:00
|
|
|
}
|
|
|
|
defer resp.Close()
|
2016-03-25 01:25:50 +00:00
|
|
|
ctx, cancelFun := context.WithCancel(context.Background())
|
2015-12-06 03:53:52 +00:00
|
|
|
cErr := promise.Go(func() error {
|
2016-03-23 11:34:47 +00:00
|
|
|
errHijack := cli.holdHijackedConnection(ctx, c.Config.Tty, in, cli.out, cli.err, resp)
|
|
|
|
if errHijack == nil {
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
return errHijack
|
2015-03-25 03:57:23 +00:00
|
|
|
})
|
|
|
|
|
2015-12-06 03:53:52 +00:00
|
|
|
// 3. Start the container.
|
2016-04-13 08:33:46 +00:00
|
|
|
if err := cli.client.ContainerStart(context.Background(), container); err != nil {
|
2016-03-25 01:25:50 +00:00
|
|
|
cancelFun()
|
|
|
|
<-cErr
|
2015-12-06 03:53:52 +00:00
|
|
|
return err
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 16:00:39 +00:00
|
|
|
// 4. Wait for attachment to break.
|
2015-12-06 03:53:52 +00:00
|
|
|
if c.Config.Tty && cli.isTerminalOut {
|
2016-04-13 08:33:46 +00:00
|
|
|
if err := cli.monitorTtySize(container, false); err != nil {
|
2015-05-09 18:33:06 +00:00
|
|
|
fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if attchErr := <-cErr; attchErr != nil {
|
|
|
|
return attchErr
|
|
|
|
}
|
2016-04-13 08:33:46 +00:00
|
|
|
_, status, err := getExitCode(cli, container)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != 0 {
|
2015-05-05 04:18:28 +00:00
|
|
|
return Cli.StatusError{StatusCode: status}
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-12-06 03:53:52 +00:00
|
|
|
} else {
|
|
|
|
// We're not going to attach to anything.
|
|
|
|
// Start as many containers as we want.
|
|
|
|
return cli.startContainersWithoutAttachments(cmd.Args())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
func (cli *DockerCli) startContainersWithoutAttachments(containers []string) error {
|
2015-12-06 03:53:52 +00:00
|
|
|
var failedContainers []string
|
2016-04-13 08:33:46 +00:00
|
|
|
for _, container := range containers {
|
|
|
|
if err := cli.client.ContainerStart(context.Background(), container); err != nil {
|
2015-12-06 03:53:52 +00:00
|
|
|
fmt.Fprintf(cli.err, "%s\n", err)
|
2016-04-13 08:33:46 +00:00
|
|
|
failedContainers = append(failedContainers, container)
|
2015-12-06 03:53:52 +00:00
|
|
|
} else {
|
2016-04-13 08:33:46 +00:00
|
|
|
fmt.Fprintf(cli.out, "%s\n", container)
|
2015-12-06 03:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(failedContainers) > 0 {
|
|
|
|
return fmt.Errorf("Error: failed to start containers: %v", strings.Join(failedContainers, ", "))
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|