Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
| 43 | 42 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,152 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ "io" |
|
| 5 |
+ "net/http/httputil" |
|
| 6 |
+ "strings" |
|
| 7 |
+ |
|
| 8 |
+ "golang.org/x/net/context" |
|
| 9 |
+ |
|
| 10 |
+ "github.com/docker/docker/api/client" |
|
| 11 |
+ "github.com/docker/docker/cli" |
|
| 12 |
+ "github.com/docker/docker/pkg/promise" |
|
| 13 |
+ "github.com/docker/docker/pkg/signal" |
|
| 14 |
+ "github.com/docker/engine-api/types" |
|
| 15 |
+ "github.com/spf13/cobra" |
|
| 16 |
+) |
|
| 17 |
+ |
|
| 18 |
+type startOptions struct {
|
|
| 19 |
+ attach bool |
|
| 20 |
+ openStdin bool |
|
| 21 |
+ detachKeys string |
|
| 22 |
+ |
|
| 23 |
+ containers []string |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+// NewStartCommand creats a new cobra.Command for `docker start` |
|
| 27 |
+func NewStartCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
| 28 |
+ var opts startOptions |
|
| 29 |
+ |
|
| 30 |
+ cmd := &cobra.Command{
|
|
| 31 |
+ Use: "start [OPTIONS] CONTAINER [CONTAINER...]", |
|
| 32 |
+ Short: "Start one or more stopped containers", |
|
| 33 |
+ Args: cli.RequiresMinArgs(1), |
|
| 34 |
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
| 35 |
+ opts.containers = args |
|
| 36 |
+ return runStart(dockerCli, &opts) |
|
| 37 |
+ }, |
|
| 38 |
+ } |
|
| 39 |
+ cmd.SetFlagErrorFunc(flagErrorFunc) |
|
| 40 |
+ |
|
| 41 |
+ flags := cmd.Flags() |
|
| 42 |
+ flags.BoolVarP(&opts.attach, "attach", "a", false, "Attach STDOUT/STDERR and forward signals") |
|
| 43 |
+ flags.BoolVarP(&opts.openStdin, "interactive", "i", false, "Attach container's STDIN") |
|
| 44 |
+ flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container") |
|
| 45 |
+ return cmd |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+func runStart(dockerCli *client.DockerCli, opts *startOptions) error {
|
|
| 49 |
+ ctx, cancelFun := context.WithCancel(context.Background()) |
|
| 50 |
+ |
|
| 51 |
+ if opts.attach || opts.openStdin {
|
|
| 52 |
+ // We're going to attach to a container. |
|
| 53 |
+ // 1. Ensure we only have one container. |
|
| 54 |
+ if len(opts.containers) > 1 {
|
|
| 55 |
+ return fmt.Errorf("You cannot start and attach multiple containers at once.")
|
|
| 56 |
+ } |
|
| 57 |
+ |
|
| 58 |
+ // 2. Attach to the container. |
|
| 59 |
+ container := opts.containers[0] |
|
| 60 |
+ c, err := dockerCli.Client().ContainerInspect(ctx, container) |
|
| 61 |
+ if err != nil {
|
|
| 62 |
+ return err |
|
| 63 |
+ } |
|
| 64 |
+ |
|
| 65 |
+ if !c.Config.Tty {
|
|
| 66 |
+ sigc := dockerCli.ForwardAllSignals(ctx, container) |
|
| 67 |
+ defer signal.StopCatch(sigc) |
|
| 68 |
+ } |
|
| 69 |
+ |
|
| 70 |
+ if opts.detachKeys != "" {
|
|
| 71 |
+ dockerCli.ConfigFile().DetachKeys = opts.detachKeys |
|
| 72 |
+ } |
|
| 73 |
+ |
|
| 74 |
+ options := types.ContainerAttachOptions{
|
|
| 75 |
+ Stream: true, |
|
| 76 |
+ Stdin: opts.openStdin && c.Config.OpenStdin, |
|
| 77 |
+ Stdout: true, |
|
| 78 |
+ Stderr: true, |
|
| 79 |
+ DetachKeys: dockerCli.ConfigFile().DetachKeys, |
|
| 80 |
+ } |
|
| 81 |
+ |
|
| 82 |
+ var in io.ReadCloser |
|
| 83 |
+ |
|
| 84 |
+ if options.Stdin {
|
|
| 85 |
+ in = dockerCli.In() |
|
| 86 |
+ } |
|
| 87 |
+ |
|
| 88 |
+ resp, errAttach := dockerCli.Client().ContainerAttach(ctx, container, options) |
|
| 89 |
+ if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
|
| 90 |
+ // ContainerAttach return an ErrPersistEOF (connection closed) |
|
| 91 |
+ // means server met an error and put it in Hijacked connection |
|
| 92 |
+ // keep the error and read detailed error message from hijacked connection |
|
| 93 |
+ return errAttach |
|
| 94 |
+ } |
|
| 95 |
+ defer resp.Close() |
|
| 96 |
+ cErr := promise.Go(func() error {
|
|
| 97 |
+ errHijack := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp) |
|
| 98 |
+ if errHijack == nil {
|
|
| 99 |
+ return errAttach |
|
| 100 |
+ } |
|
| 101 |
+ return errHijack |
|
| 102 |
+ }) |
|
| 103 |
+ |
|
| 104 |
+ // 3. Start the container. |
|
| 105 |
+ if err := dockerCli.Client().ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
|
|
| 106 |
+ cancelFun() |
|
| 107 |
+ <-cErr |
|
| 108 |
+ return err |
|
| 109 |
+ } |
|
| 110 |
+ |
|
| 111 |
+ // 4. Wait for attachment to break. |
|
| 112 |
+ if c.Config.Tty && dockerCli.IsTerminalOut() {
|
|
| 113 |
+ if err := dockerCli.MonitorTtySize(ctx, container, false); err != nil {
|
|
| 114 |
+ fmt.Fprintf(dockerCli.Err(), "Error monitoring TTY size: %s\n", err) |
|
| 115 |
+ } |
|
| 116 |
+ } |
|
| 117 |
+ if attchErr := <-cErr; attchErr != nil {
|
|
| 118 |
+ return attchErr |
|
| 119 |
+ } |
|
| 120 |
+ _, status, err := dockerCli.GetExitCode(ctx, container) |
|
| 121 |
+ if err != nil {
|
|
| 122 |
+ return err |
|
| 123 |
+ } |
|
| 124 |
+ if status != 0 {
|
|
| 125 |
+ return cli.StatusError{StatusCode: status}
|
|
| 126 |
+ } |
|
| 127 |
+ } else {
|
|
| 128 |
+ // We're not going to attach to anything. |
|
| 129 |
+ // Start as many containers as we want. |
|
| 130 |
+ return startContainersWithoutAttachments(dockerCli, ctx, opts.containers) |
|
| 131 |
+ } |
|
| 132 |
+ |
|
| 133 |
+ return nil |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+func startContainersWithoutAttachments(dockerCli *client.DockerCli, ctx context.Context, containers []string) error {
|
|
| 137 |
+ var failedContainers []string |
|
| 138 |
+ for _, container := range containers {
|
|
| 139 |
+ if err := dockerCli.Client().ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
|
|
| 140 |
+ fmt.Fprintf(dockerCli.Err(), "%s\n", err) |
|
| 141 |
+ failedContainers = append(failedContainers, container) |
|
| 142 |
+ } else {
|
|
| 143 |
+ fmt.Fprintf(dockerCli.Out(), "%s\n", container) |
|
| 144 |
+ } |
|
| 145 |
+ } |
|
| 146 |
+ |
|
| 147 |
+ if len(failedContainers) > 0 {
|
|
| 148 |
+ return fmt.Errorf("Error: failed to start containers: %v", strings.Join(failedContainers, ", "))
|
|
| 149 |
+ } |
|
| 150 |
+ return nil |
|
| 151 |
+} |
| 0 | 152 |
deleted file mode 100644 |
| ... | ... |
@@ -1,165 +0,0 @@ |
| 1 |
-package client |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "io" |
|
| 6 |
- "net/http/httputil" |
|
| 7 |
- "os" |
|
| 8 |
- "strings" |
|
| 9 |
- |
|
| 10 |
- "golang.org/x/net/context" |
|
| 11 |
- |
|
| 12 |
- "github.com/Sirupsen/logrus" |
|
| 13 |
- Cli "github.com/docker/docker/cli" |
|
| 14 |
- flag "github.com/docker/docker/pkg/mflag" |
|
| 15 |
- "github.com/docker/docker/pkg/promise" |
|
| 16 |
- "github.com/docker/docker/pkg/signal" |
|
| 17 |
- "github.com/docker/engine-api/types" |
|
| 18 |
-) |
|
| 19 |
- |
|
| 20 |
-// ForwardAllSignals forwards signals to the contianer |
|
| 21 |
-// TODO: this can be unexported again once all container commands are under |
|
| 22 |
-// api/client/container |
|
| 23 |
-func (cli *DockerCli) ForwardAllSignals(ctx context.Context, cid string) chan os.Signal {
|
|
| 24 |
- sigc := make(chan os.Signal, 128) |
|
| 25 |
- signal.CatchAll(sigc) |
|
| 26 |
- go func() {
|
|
| 27 |
- for s := range sigc {
|
|
| 28 |
- if s == signal.SIGCHLD || s == signal.SIGPIPE {
|
|
| 29 |
- continue |
|
| 30 |
- } |
|
| 31 |
- var sig string |
|
| 32 |
- for sigStr, sigN := range signal.SignalMap {
|
|
| 33 |
- if sigN == s {
|
|
| 34 |
- sig = sigStr |
|
| 35 |
- break |
|
| 36 |
- } |
|
| 37 |
- } |
|
| 38 |
- if sig == "" {
|
|
| 39 |
- fmt.Fprintf(cli.err, "Unsupported signal: %v. Discarding.\n", s) |
|
| 40 |
- continue |
|
| 41 |
- } |
|
| 42 |
- |
|
| 43 |
- if err := cli.client.ContainerKill(ctx, cid, sig); err != nil {
|
|
| 44 |
- logrus.Debugf("Error sending signal: %s", err)
|
|
| 45 |
- } |
|
| 46 |
- } |
|
| 47 |
- }() |
|
| 48 |
- return sigc |
|
| 49 |
-} |
|
| 50 |
- |
|
| 51 |
-// CmdStart starts one or more containers. |
|
| 52 |
-// |
|
| 53 |
-// Usage: docker start [OPTIONS] CONTAINER [CONTAINER...] |
|
| 54 |
-func (cli *DockerCli) CmdStart(args ...string) error {
|
|
| 55 |
- cmd := Cli.Subcmd("start", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["start"].Description, true)
|
|
| 56 |
- attach := cmd.Bool([]string{"a", "-attach"}, false, "Attach STDOUT/STDERR and forward signals")
|
|
| 57 |
- openStdin := cmd.Bool([]string{"i", "-interactive"}, false, "Attach container's STDIN")
|
|
| 58 |
- detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
|
|
| 59 |
- cmd.Require(flag.Min, 1) |
|
| 60 |
- |
|
| 61 |
- cmd.ParseFlags(args, true) |
|
| 62 |
- |
|
| 63 |
- ctx, cancelFun := context.WithCancel(context.Background()) |
|
| 64 |
- |
|
| 65 |
- if *attach || *openStdin {
|
|
| 66 |
- // We're going to attach to a container. |
|
| 67 |
- // 1. Ensure we only have one container. |
|
| 68 |
- if cmd.NArg() > 1 {
|
|
| 69 |
- return fmt.Errorf("You cannot start and attach multiple containers at once.")
|
|
| 70 |
- } |
|
| 71 |
- |
|
| 72 |
- // 2. Attach to the container. |
|
| 73 |
- container := cmd.Arg(0) |
|
| 74 |
- c, err := cli.client.ContainerInspect(ctx, container) |
|
| 75 |
- if err != nil {
|
|
| 76 |
- return err |
|
| 77 |
- } |
|
| 78 |
- |
|
| 79 |
- if !c.Config.Tty {
|
|
| 80 |
- sigc := cli.ForwardAllSignals(ctx, container) |
|
| 81 |
- defer signal.StopCatch(sigc) |
|
| 82 |
- } |
|
| 83 |
- |
|
| 84 |
- if *detachKeys != "" {
|
|
| 85 |
- cli.configFile.DetachKeys = *detachKeys |
|
| 86 |
- } |
|
| 87 |
- |
|
| 88 |
- options := types.ContainerAttachOptions{
|
|
| 89 |
- Stream: true, |
|
| 90 |
- Stdin: *openStdin && c.Config.OpenStdin, |
|
| 91 |
- Stdout: true, |
|
| 92 |
- Stderr: true, |
|
| 93 |
- DetachKeys: cli.configFile.DetachKeys, |
|
| 94 |
- } |
|
| 95 |
- |
|
| 96 |
- var in io.ReadCloser |
|
| 97 |
- |
|
| 98 |
- if options.Stdin {
|
|
| 99 |
- in = cli.in |
|
| 100 |
- } |
|
| 101 |
- |
|
| 102 |
- resp, errAttach := cli.client.ContainerAttach(ctx, container, options) |
|
| 103 |
- if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
|
| 104 |
- // ContainerAttach return an ErrPersistEOF (connection closed) |
|
| 105 |
- // means server met an error and put it in Hijacked connection |
|
| 106 |
- // keep the error and read detailed error message from hijacked connection |
|
| 107 |
- return errAttach |
|
| 108 |
- } |
|
| 109 |
- defer resp.Close() |
|
| 110 |
- cErr := promise.Go(func() error {
|
|
| 111 |
- errHijack := cli.HoldHijackedConnection(ctx, c.Config.Tty, in, cli.out, cli.err, resp) |
|
| 112 |
- if errHijack == nil {
|
|
| 113 |
- return errAttach |
|
| 114 |
- } |
|
| 115 |
- return errHijack |
|
| 116 |
- }) |
|
| 117 |
- |
|
| 118 |
- // 3. Start the container. |
|
| 119 |
- if err := cli.client.ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
|
|
| 120 |
- cancelFun() |
|
| 121 |
- <-cErr |
|
| 122 |
- return err |
|
| 123 |
- } |
|
| 124 |
- |
|
| 125 |
- // 4. Wait for attachment to break. |
|
| 126 |
- if c.Config.Tty && cli.isTerminalOut {
|
|
| 127 |
- if err := cli.MonitorTtySize(ctx, container, false); err != nil {
|
|
| 128 |
- fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err) |
|
| 129 |
- } |
|
| 130 |
- } |
|
| 131 |
- if attchErr := <-cErr; attchErr != nil {
|
|
| 132 |
- return attchErr |
|
| 133 |
- } |
|
| 134 |
- _, status, err := cli.GetExitCode(ctx, container) |
|
| 135 |
- if err != nil {
|
|
| 136 |
- return err |
|
| 137 |
- } |
|
| 138 |
- if status != 0 {
|
|
| 139 |
- return Cli.StatusError{StatusCode: status}
|
|
| 140 |
- } |
|
| 141 |
- } else {
|
|
| 142 |
- // We're not going to attach to anything. |
|
| 143 |
- // Start as many containers as we want. |
|
| 144 |
- return cli.startContainersWithoutAttachments(ctx, cmd.Args()) |
|
| 145 |
- } |
|
| 146 |
- |
|
| 147 |
- return nil |
|
| 148 |
-} |
|
| 149 |
- |
|
| 150 |
-func (cli *DockerCli) startContainersWithoutAttachments(ctx context.Context, containers []string) error {
|
|
| 151 |
- var failedContainers []string |
|
| 152 |
- for _, container := range containers {
|
|
| 153 |
- if err := cli.client.ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
|
|
| 154 |
- fmt.Fprintf(cli.err, "%s\n", err) |
|
| 155 |
- failedContainers = append(failedContainers, container) |
|
| 156 |
- } else {
|
|
| 157 |
- fmt.Fprintf(cli.out, "%s\n", container) |
|
| 158 |
- } |
|
| 159 |
- } |
|
| 160 |
- |
|
| 161 |
- if len(failedContainers) > 0 {
|
|
| 162 |
- return fmt.Errorf("Error: failed to start containers: %v", strings.Join(failedContainers, ", "))
|
|
| 163 |
- } |
|
| 164 |
- return nil |
|
| 165 |
-} |
| ... | ... |
@@ -204,3 +204,34 @@ func (cli *DockerCli) retrieveAuthConfigs() map[string]types.AuthConfig {
|
| 204 | 204 |
acs, _ := getAllCredentials(cli.configFile) |
| 205 | 205 |
return acs |
| 206 | 206 |
} |
| 207 |
+ |
|
| 208 |
+// ForwardAllSignals forwards signals to the contianer |
|
| 209 |
+// TODO: this can be unexported again once all container commands are under |
|
| 210 |
+// api/client/container |
|
| 211 |
+func (cli *DockerCli) ForwardAllSignals(ctx context.Context, cid string) chan os.Signal {
|
|
| 212 |
+ sigc := make(chan os.Signal, 128) |
|
| 213 |
+ signal.CatchAll(sigc) |
|
| 214 |
+ go func() {
|
|
| 215 |
+ for s := range sigc {
|
|
| 216 |
+ if s == signal.SIGCHLD || s == signal.SIGPIPE {
|
|
| 217 |
+ continue |
|
| 218 |
+ } |
|
| 219 |
+ var sig string |
|
| 220 |
+ for sigStr, sigN := range signal.SignalMap {
|
|
| 221 |
+ if sigN == s {
|
|
| 222 |
+ sig = sigStr |
|
| 223 |
+ break |
|
| 224 |
+ } |
|
| 225 |
+ } |
|
| 226 |
+ if sig == "" {
|
|
| 227 |
+ fmt.Fprintf(cli.err, "Unsupported signal: %v. Discarding.\n", s) |
|
| 228 |
+ continue |
|
| 229 |
+ } |
|
| 230 |
+ |
|
| 231 |
+ if err := cli.client.ContainerKill(ctx, cid, sig); err != nil {
|
|
| 232 |
+ logrus.Debugf("Error sending signal: %s", err)
|
|
| 233 |
+ } |
|
| 234 |
+ } |
|
| 235 |
+ }() |
|
| 236 |
+ return sigc |
|
| 237 |
+} |
| ... | ... |
@@ -36,6 +36,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
| 36 | 36 |
container.NewCreateCommand(dockerCli), |
| 37 | 37 |
container.NewExportCommand(dockerCli), |
| 38 | 38 |
container.NewRunCommand(dockerCli), |
| 39 |
+ container.NewStartCommand(dockerCli), |
|
| 39 | 40 |
container.NewStopCommand(dockerCli), |
| 40 | 41 |
image.NewRemoveCommand(dockerCli), |
| 41 | 42 |
image.NewSearchCommand(dockerCli), |
| ... | ... |
@@ -35,7 +35,6 @@ var DockerCommandUsage = []Command{
|
| 35 | 35 |
{"restart", "Restart a container"},
|
| 36 | 36 |
{"rm", "Remove one or more containers"},
|
| 37 | 37 |
{"save", "Save one or more images to a tar archive"},
|
| 38 |
- {"start", "Start one or more stopped containers"},
|
|
| 39 | 38 |
{"stats", "Display a live stream of container(s) resource usage statistics"},
|
| 40 | 39 |
{"tag", "Tag an image into a repository"},
|
| 41 | 40 |
{"top", "Display the running processes of a container"},
|