2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2015-12-05 22:53:52 -05:00
|
|
|
"strings"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
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 {
|
|
|
|
if s == signal.SIGCHLD {
|
|
|
|
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
|
|
|
|
|
|
|
if err := cli.client.ContainerKill(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)
|
|
|
|
c, err := cli.client.ContainerInspect(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
|
2015-12-05 22:53:52 -05:00
|
|
|
if options.Stdin {
|
2015-03-24 23:57:23 -04:00
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:53:52 -05:00
|
|
|
resp, err := cli.client.ContainerAttach(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-05 22:53:52 -05:00
|
|
|
cErr := promise.Go(func() error {
|
|
|
|
return cli.holdHijackedConnection(c.Config.Tty, in, cli.out, cli.err, resp)
|
2015-03-24 23:57:23 -04:00
|
|
|
})
|
|
|
|
|
2015-12-05 22:53:52 -05:00
|
|
|
// 3. Start the container.
|
|
|
|
if err := cli.client.ContainerStart(containerID); err != nil {
|
|
|
|
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 {
|
|
|
|
if err := cli.client.ContainerStart(containerID); err != nil {
|
|
|
|
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
|
|
|
|
}
|