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