2016-05-31 16:49:32 -07:00
|
|
|
package container
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-03-23 19:34:47 +08:00
|
|
|
"net/http/httputil"
|
2015-03-24 23:57:23 -04:00
|
|
|
"os"
|
2015-05-27 13:15:14 -07:00
|
|
|
"runtime"
|
2015-07-29 08:21:16 -04:00
|
|
|
"strings"
|
2016-05-31 23:18:17 +08:00
|
|
|
"syscall"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-03-26 23:22:04 +01:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-31 16:49:32 -07:00
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
opttypes "github.com/docker/docker/opts"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-12-21 20:05:55 -05:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-05-06 22:39:29 +00:00
|
|
|
"github.com/docker/libnetwork/resolvconf/dns"
|
2016-05-31 16:49:32 -07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
const (
|
2016-03-24 08:26:04 -04:00
|
|
|
errCmdNotFound = "not found or does not exist"
|
|
|
|
errCmdCouldNotBeInvoked = "could not be invoked"
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
)
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
type runOptions struct {
|
|
|
|
autoRemove bool
|
|
|
|
detach bool
|
|
|
|
sigProxy bool
|
|
|
|
name string
|
|
|
|
detachKeys string
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
// NewRunCommand create a new `docker run` command
|
|
|
|
func NewRunCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
var opts runOptions
|
|
|
|
var copts *runconfigopts.ContainerOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "run [OPTIONS] IMAGE [COMMAND] [ARG...]",
|
|
|
|
Short: "Run a command in a new container",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
copts.Image = args[0]
|
|
|
|
if len(args) > 1 {
|
|
|
|
copts.Args = args[1:]
|
|
|
|
}
|
|
|
|
return runRun(dockerCli, cmd.Flags(), &opts, copts)
|
|
|
|
},
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-05-31 22:19:13 -07:00
|
|
|
cmd.SetFlagErrorFunc(flagErrorFunc)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SetInterspersed(false)
|
2016-03-19 15:01:15 -07:00
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
// These are flags not stored in Config/HostConfig
|
|
|
|
flags.BoolVar(&opts.autoRemove, "rm", false, "Automatically remove the container when it exits")
|
|
|
|
flags.BoolVarP(&opts.detach, "detach", "d", false, "Run container in background and print container ID")
|
|
|
|
flags.BoolVar(&opts.sigProxy, "sig-proxy", true, "Proxy received signals to the process")
|
|
|
|
flags.StringVar(&opts.name, "name", "", "Assign a name to the container")
|
|
|
|
flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
|
|
|
|
|
|
|
|
// Add an explicit help that doesn't have a `-h` to prevent the conflict
|
|
|
|
// with hostname
|
|
|
|
flags.Bool("help", false, "Print usage")
|
|
|
|
|
|
|
|
client.AddTrustedFlags(flags, true)
|
|
|
|
copts = runconfigopts.AddFlags(flags)
|
|
|
|
return cmd
|
2015-07-29 08:21:16 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 22:19:13 -07:00
|
|
|
func flagErrorFunc(cmd *cobra.Command, err error) error {
|
|
|
|
return cli.StatusError{
|
|
|
|
Status: cli.FlagErrorFunc(cmd, err).Error(),
|
|
|
|
StatusCode: 125,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
func runRun(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts *runOptions, copts *runconfigopts.ContainerOptions) error {
|
|
|
|
stdout, stderr, stdin := dockerCli.Out(), dockerCli.Err(), dockerCli.In()
|
|
|
|
client := dockerCli.Client()
|
|
|
|
// TODO: pass this as an argument
|
|
|
|
cmdPath := "run"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
var (
|
2016-05-31 16:49:32 -07:00
|
|
|
flAttach *opttypes.ListOpts
|
2015-03-24 23:57:23 -04:00
|
|
|
ErrConflictAttachDetach = fmt.Errorf("Conflicting options: -a and -d")
|
|
|
|
ErrConflictRestartPolicyAndAutoRemove = fmt.Errorf("Conflicting options: --restart and --rm")
|
|
|
|
ErrConflictDetachAutoRemove = fmt.Errorf("Conflicting options: --rm and -d")
|
|
|
|
)
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
config, hostConfig, networkingConfig, err := runconfigopts.Parse(flags, copts)
|
2016-01-07 16:18:34 -08:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
// just in case the Parse does not exit
|
|
|
|
if err != nil {
|
2016-05-31 16:49:32 -07:00
|
|
|
reportError(stderr, cmdPath, err.Error(), true)
|
2016-05-31 22:19:13 -07:00
|
|
|
return cli.StatusError{StatusCode: 125}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-12-31 14:17:18 +08:00
|
|
|
if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
|
2016-05-31 16:49:32 -07:00
|
|
|
fmt.Fprintf(stderr, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n")
|
2015-10-26 11:33:51 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 11:11:45 +02:00
|
|
|
if len(hostConfig.DNS) > 0 {
|
2015-03-24 23:57:23 -04:00
|
|
|
// check the DNS settings passed via --dns against
|
|
|
|
// localhost regexp to warn if they are trying to
|
|
|
|
// set a DNS to a localhost address
|
2015-07-25 11:11:45 +02:00
|
|
|
for _, dnsIP := range hostConfig.DNS {
|
2015-05-11 15:16:23 -07:00
|
|
|
if dns.IsLocalhost(dnsIP) {
|
2016-05-31 16:49:32 -07:00
|
|
|
fmt.Fprintf(stderr, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
|
2015-03-24 23:57:23 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 11:49:16 -08:00
|
|
|
config.ArgsEscaped = false
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
if !opts.detach {
|
|
|
|
if err := dockerCli.CheckTtyInput(config.AttachStdin, config.Tty); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-31 16:49:32 -07:00
|
|
|
if fl := flags.Lookup("attach"); fl != nil {
|
|
|
|
flAttach = fl.Value.(*opttypes.ListOpts)
|
2015-03-24 23:57:23 -04:00
|
|
|
if flAttach.Len() != 0 {
|
|
|
|
return ErrConflictAttachDetach
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 16:49:32 -07:00
|
|
|
if opts.autoRemove {
|
2015-03-24 23:57:23 -04:00
|
|
|
return ErrConflictDetachAutoRemove
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AttachStdin = false
|
|
|
|
config.AttachStdout = false
|
|
|
|
config.AttachStderr = false
|
|
|
|
config.StdinOnce = false
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
// Disable sigProxy when in TTY mode
|
2015-03-24 23:57:23 -04:00
|
|
|
if config.Tty {
|
2016-05-31 16:49:32 -07:00
|
|
|
opts.sigProxy = false
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 13:15:14 -07:00
|
|
|
// Telling the Windows daemon the initial size of the tty during start makes
|
|
|
|
// a far better user experience rather than relying on subsequent resizes
|
|
|
|
// to cause things to catch up.
|
|
|
|
if runtime.GOOS == "windows" {
|
2016-05-31 16:49:32 -07:00
|
|
|
hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = dockerCli.GetTtySize()
|
2015-05-27 13:15:14 -07:00
|
|
|
}
|
|
|
|
|
2016-05-21 15:57:57 +02:00
|
|
|
ctx, cancelFun := context.WithCancel(context.Background())
|
|
|
|
|
2016-05-31 22:19:13 -07:00
|
|
|
createResponse, err := createContainer(ctx, dockerCli, config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, opts.name)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
2016-05-31 16:49:32 -07:00
|
|
|
reportError(stderr, cmdPath, err.Error(), true)
|
2015-07-29 08:21:16 -04:00
|
|
|
return runStartContainerErr(err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-05-31 16:49:32 -07:00
|
|
|
if opts.sigProxy {
|
|
|
|
sigc := dockerCli.ForwardAllSignals(ctx, createResponse.ID)
|
2015-03-24 23:57:23 -04:00
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
var (
|
2015-03-25 19:31:29 -07:00
|
|
|
waitDisplayID chan struct{}
|
2015-03-24 23:57:23 -04:00
|
|
|
errCh chan error
|
|
|
|
)
|
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
|
|
// Make this asynchronous to allow the client to write to stdin before having to read the ID
|
2015-03-25 19:31:29 -07:00
|
|
|
waitDisplayID = make(chan struct{})
|
2015-03-24 23:57:23 -04:00
|
|
|
go func() {
|
2015-03-25 19:31:29 -07:00
|
|
|
defer close(waitDisplayID)
|
2016-05-31 16:49:32 -07:00
|
|
|
fmt.Fprintf(stdout, "%s\n", createResponse.ID)
|
2015-03-24 23:57:23 -04:00
|
|
|
}()
|
|
|
|
}
|
2016-05-31 16:49:32 -07:00
|
|
|
if opts.autoRemove && (hostConfig.RestartPolicy.IsAlways() || hostConfig.RestartPolicy.IsOnFailure()) {
|
2015-03-24 23:57:23 -04:00
|
|
|
return ErrConflictRestartPolicyAndAutoRemove
|
|
|
|
}
|
2016-03-24 21:25:50 -04:00
|
|
|
attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
|
|
|
|
if attach {
|
2015-03-24 23:57:23 -04:00
|
|
|
var (
|
2016-05-31 16:49:32 -07:00
|
|
|
out, cerr io.Writer
|
|
|
|
in io.ReadCloser
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
if config.AttachStdin {
|
2016-05-31 16:49:32 -07:00
|
|
|
in = stdin
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
if config.AttachStdout {
|
2016-05-31 16:49:32 -07:00
|
|
|
out = stdout
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
if config.AttachStderr {
|
|
|
|
if config.Tty {
|
2016-05-31 16:49:32 -07:00
|
|
|
cerr = stdout
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2016-05-31 16:49:32 -07:00
|
|
|
cerr = stderr
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-12-05 23:18:06 -05:00
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
if opts.detachKeys != "" {
|
|
|
|
dockerCli.ConfigFile().DetachKeys = opts.detachKeys
|
2016-01-03 23:03:39 +01:00
|
|
|
}
|
|
|
|
|
2015-12-05 23:18:06 -05:00
|
|
|
options := types.ContainerAttachOptions{
|
2016-04-13 10:33:46 +02:00
|
|
|
Stream: true,
|
|
|
|
Stdin: config.AttachStdin,
|
|
|
|
Stdout: config.AttachStdout,
|
|
|
|
Stderr: config.AttachStderr,
|
2016-05-31 16:49:32 -07:00
|
|
|
DetachKeys: dockerCli.ConfigFile().DetachKeys,
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-05 23:18:06 -05:00
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
resp, errAttach := client.ContainerAttach(ctx, createResponse.ID, options)
|
2016-03-23 19:34:47 +08:00
|
|
|
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
|
|
|
// ContainerAttach returns 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 later
|
|
|
|
return errAttach
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-04-27 16:48:21 +08:00
|
|
|
defer resp.Close()
|
|
|
|
|
2015-12-05 23:18:06 -05:00
|
|
|
errCh = promise.Go(func() error {
|
2016-05-31 16:49:32 -07:00
|
|
|
errHijack := dockerCli.HoldHijackedConnection(ctx, config.Tty, in, out, cerr, resp)
|
2016-03-23 19:34:47 +08:00
|
|
|
if errHijack == nil {
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
return errHijack
|
2015-12-05 23:18:06 -05:00
|
|
|
})
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
if opts.autoRemove {
|
2016-02-01 15:30:58 -05:00
|
|
|
defer func() {
|
2016-05-21 15:57:57 +02:00
|
|
|
// Explicitly not sharing the context as it could be "Done" (by calling cancelFun)
|
|
|
|
// and thus the container would not be removed.
|
2016-06-06 23:32:38 +08:00
|
|
|
if err := removeContainer(dockerCli, context.Background(), createResponse.ID, true, false, true); err != nil {
|
2016-05-31 16:49:32 -07:00
|
|
|
fmt.Fprintf(stderr, "%v\n", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-02-01 15:30:58 -05:00
|
|
|
}()
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
//start the container
|
2016-05-31 16:49:32 -07:00
|
|
|
if err := client.ContainerStart(ctx, createResponse.ID, types.ContainerStartOptions{}); err != nil {
|
2016-03-24 21:25:50 -04:00
|
|
|
// If we have holdHijackedConnection, we should notify
|
|
|
|
// holdHijackedConnection we are going to exit and wait
|
|
|
|
// to avoid the terminal are not restored.
|
|
|
|
if attach {
|
|
|
|
cancelFun()
|
|
|
|
<-errCh
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
reportError(stderr, cmdPath, err.Error(), false)
|
2015-07-29 08:21:16 -04:00
|
|
|
return runStartContainerErr(err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 16:49:32 -07:00
|
|
|
if (config.AttachStdin || config.AttachStdout || config.AttachStderr) && config.Tty && dockerCli.IsTerminalOut() {
|
|
|
|
if err := dockerCli.MonitorTtySize(ctx, createResponse.ID, false); err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Error monitoring TTY size: %s\n", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errCh != nil {
|
|
|
|
if err := <-errCh; err != nil {
|
2015-03-26 23:22:04 +01:00
|
|
|
logrus.Debugf("Error hijack: %s", err)
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detached mode: wait for the id to be displayed and return.
|
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
|
|
// Detached mode
|
2015-03-25 19:31:29 -07:00
|
|
|
<-waitDisplayID
|
2015-03-24 23:57:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var status int
|
|
|
|
|
|
|
|
// Attached mode
|
2016-05-31 16:49:32 -07:00
|
|
|
if opts.autoRemove {
|
2015-03-24 23:57:23 -04:00
|
|
|
// Autoremove: wait for the container to finish, retrieve
|
|
|
|
// the exit code and remove the container
|
2016-05-31 16:49:32 -07:00
|
|
|
if status, err = client.ContainerWait(ctx, createResponse.ID); err != nil {
|
2015-07-29 08:21:16 -04:00
|
|
|
return runStartContainerErr(err)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-06-09 18:04:53 +08:00
|
|
|
if _, status, err = getExitCode(dockerCli, ctx, createResponse.ID); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No Autoremove: Simply retrieve the exit code
|
2016-05-17 10:30:06 +08:00
|
|
|
if !config.Tty && hostConfig.RestartPolicy.IsNone() {
|
2015-03-24 23:57:23 -04:00
|
|
|
// In non-TTY mode, we can't detach, so we must wait for container exit
|
2016-05-31 16:49:32 -07:00
|
|
|
if status, err = client.ContainerWait(ctx, createResponse.ID); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In TTY mode, there is a race: if the process dies too slowly, the state could
|
|
|
|
// be updated after the getExitCode call and result in the wrong exit code being reported
|
2016-06-09 18:04:53 +08:00
|
|
|
if _, status, err = getExitCode(dockerCli, ctx, createResponse.ID); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if status != 0 {
|
2016-05-31 16:49:32 -07:00
|
|
|
return cli.StatusError{StatusCode: status}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-31 16:49:32 -07:00
|
|
|
|
|
|
|
// reportError is a utility method that prints a user-friendly message
|
|
|
|
// containing the error that occurred during parsing and a suggestion to get help
|
|
|
|
func reportError(stderr io.Writer, name string, str string, withHelp bool) {
|
|
|
|
if withHelp {
|
|
|
|
str += ".\nSee '" + os.Args[0] + " " + name + " --help'"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(stderr, "%s: %s.\n", os.Args[0], str)
|
|
|
|
}
|
|
|
|
|
2016-05-31 23:18:17 +08:00
|
|
|
// if container start fails with 'not found'/'no such' error, return 127
|
|
|
|
// if container start fails with 'permission denied' error, return 126
|
2016-05-31 16:49:32 -07:00
|
|
|
// return 125 for generic docker daemon failures
|
|
|
|
func runStartContainerErr(err error) error {
|
|
|
|
trimmedErr := strings.TrimPrefix(err.Error(), "Error response from daemon: ")
|
|
|
|
statusError := cli.StatusError{StatusCode: 125}
|
2016-05-31 23:18:17 +08:00
|
|
|
if strings.Contains(trimmedErr, "executable file not found") ||
|
|
|
|
strings.Contains(trimmedErr, "no such file or directory") ||
|
|
|
|
strings.Contains(trimmedErr, "system cannot find the file specified") {
|
|
|
|
statusError = cli.StatusError{StatusCode: 127}
|
|
|
|
} else if strings.Contains(trimmedErr, syscall.EACCES.Error()) {
|
|
|
|
statusError = cli.StatusError{StatusCode: 126}
|
2016-05-31 16:49:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return statusError
|
|
|
|
}
|