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-05-27 20:15:14 +00:00
|
|
|
"runtime"
|
2015-07-29 12:21:16 +00:00
|
|
|
"strings"
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2016-02-03 23:41:26 +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
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-12-22 01:05:55 +00:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-05-06 22:39:29 +00:00
|
|
|
"github.com/docker/libnetwork/resolvconf/dns"
|
2015-03-25 03:57:23 +00: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 15:53:35 +00:00
|
|
|
const (
|
2016-03-24 12:26:04 +00: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 15:53:35 +00:00
|
|
|
)
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cid *cidFile) Close() error {
|
|
|
|
cid.file.Close()
|
|
|
|
|
|
|
|
if !cid.written {
|
|
|
|
if err := os.Remove(cid.path); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cid *cidFile) Write(id string) error {
|
|
|
|
if _, err := cid.file.Write([]byte(id)); err != nil {
|
|
|
|
return fmt.Errorf("Failed to write the container ID to the file: %s", err)
|
|
|
|
}
|
|
|
|
cid.written = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-29 12:21:16 +00:00
|
|
|
// if container start fails with 'command not found' error, return 127
|
|
|
|
// if container start fails with 'command cannot be invoked' error, return 126
|
|
|
|
// return 125 for generic docker daemon failures
|
|
|
|
func runStartContainerErr(err error) error {
|
2016-03-24 12:26:04 +00:00
|
|
|
trimmedErr := strings.TrimPrefix(err.Error(), "Error response from daemon: ")
|
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 15:53:35 +00:00
|
|
|
statusError := Cli.StatusError{StatusCode: 125}
|
2016-03-19 22:01:15 +00:00
|
|
|
if strings.HasPrefix(trimmedErr, "Container command") {
|
|
|
|
if strings.Contains(trimmedErr, errCmdNotFound) {
|
|
|
|
statusError = Cli.StatusError{StatusCode: 127}
|
|
|
|
} else if strings.Contains(trimmedErr, errCmdCouldNotBeInvoked) {
|
|
|
|
statusError = Cli.StatusError{StatusCode: 126}
|
|
|
|
}
|
2015-07-29 12:21:16 +00:00
|
|
|
}
|
2016-03-19 22:01:15 +00:00
|
|
|
|
2015-07-29 12:21:16 +00:00
|
|
|
return statusError
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdRun runs a command in a new container.
|
|
|
|
//
|
|
|
|
// Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdRun(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("run", []string{"IMAGE [COMMAND] [ARG...]"}, Cli.DockerCommands["run"].Description, true)
|
2015-07-15 20:42:45 +00:00
|
|
|
addTrustedFlags(cmd, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
// These are flags not stored in Config/HostConfig
|
|
|
|
var (
|
2015-04-21 00:13:48 +00:00
|
|
|
flAutoRemove = cmd.Bool([]string{"-rm"}, false, "Automatically remove the container when it exits")
|
2015-03-25 03:57:23 +00:00
|
|
|
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Run container in background and print container ID")
|
2015-04-21 00:13:48 +00:00
|
|
|
flSigProxy = cmd.Bool([]string{"-sig-proxy"}, true, "Proxy received signals to the process")
|
|
|
|
flName = cmd.String([]string{"-name"}, "", "Assign a name to the container")
|
2016-01-03 22:03:39 +00:00
|
|
|
flDetachKeys = cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
|
2015-03-25 03:57:23 +00:00
|
|
|
flAttach *opts.ListOpts
|
|
|
|
|
|
|
|
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-01-08 00:18:34 +00:00
|
|
|
config, hostConfig, networkingConfig, cmd, err := runconfigopts.Parse(cmd, args)
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
// just in case the Parse does not exit
|
|
|
|
if err != nil {
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ReportError(err.Error(), true)
|
2015-07-29 12:21:16 +00:00
|
|
|
os.Exit(125)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 06:17:18 +00:00
|
|
|
if hostConfig.OomKillDisable != nil && *hostConfig.OomKillDisable && hostConfig.Memory == 0 {
|
2016-01-13 18:40:49 +00:00
|
|
|
fmt.Fprintf(cli.err, "WARNING: Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.\n")
|
2015-10-26 03:33:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
if len(hostConfig.DNS) > 0 {
|
2015-03-25 03:57:23 +00: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 09:11:45 +00:00
|
|
|
for _, dnsIP := range hostConfig.DNS {
|
2015-05-11 22:16:23 +00:00
|
|
|
if dns.IsLocalhost(dnsIP) {
|
2015-03-25 03:57:23 +00:00
|
|
|
fmt.Fprintf(cli.err, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.Image == "" {
|
|
|
|
cmd.Usage()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:49:16 +00:00
|
|
|
config.ArgsEscaped = false
|
|
|
|
|
2015-03-25 03:57:23 +00:00
|
|
|
if !*flDetach {
|
|
|
|
if err := cli.CheckTtyInput(config.AttachStdin, config.Tty); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if fl := cmd.Lookup("-attach"); fl != nil {
|
|
|
|
flAttach = fl.Value.(*opts.ListOpts)
|
|
|
|
if flAttach.Len() != 0 {
|
|
|
|
return ErrConflictAttachDetach
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *flAutoRemove {
|
|
|
|
return ErrConflictDetachAutoRemove
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AttachStdin = false
|
|
|
|
config.AttachStdout = false
|
|
|
|
config.AttachStderr = false
|
|
|
|
config.StdinOnce = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable flSigProxy when in TTY mode
|
|
|
|
sigProxy := *flSigProxy
|
|
|
|
if config.Tty {
|
|
|
|
sigProxy = false
|
|
|
|
}
|
|
|
|
|
2015-05-27 20:15:14 +00: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" {
|
|
|
|
hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = cli.getTtySize()
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:18:34 +00:00
|
|
|
createResponse, err := cli.createContainer(config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, *flName)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
2015-07-29 12:21:16 +00:00
|
|
|
cmd.ReportError(err.Error(), true)
|
|
|
|
return runStartContainerErr(err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
if sigProxy {
|
|
|
|
sigc := cli.forwardAllSignals(createResponse.ID)
|
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
var (
|
2015-03-26 02:31:29 +00:00
|
|
|
waitDisplayID chan struct{}
|
2015-03-25 03:57:23 +00:00
|
|
|
errCh chan error
|
2016-03-25 01:25:50 +00:00
|
|
|
cancelFun context.CancelFunc
|
|
|
|
ctx context.Context
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
|
|
// Make this asynchronous to allow the client to write to stdin before having to read the ID
|
2015-03-26 02:31:29 +00:00
|
|
|
waitDisplayID = make(chan struct{})
|
2015-03-25 03:57:23 +00:00
|
|
|
go func() {
|
2015-03-26 02:31:29 +00:00
|
|
|
defer close(waitDisplayID)
|
2015-03-25 03:57:23 +00:00
|
|
|
fmt.Fprintf(cli.out, "%s\n", createResponse.ID)
|
|
|
|
}()
|
|
|
|
}
|
2015-05-16 11:34:09 +00:00
|
|
|
if *flAutoRemove && (hostConfig.RestartPolicy.IsAlways() || hostConfig.RestartPolicy.IsOnFailure()) {
|
2015-03-25 03:57:23 +00:00
|
|
|
return ErrConflictRestartPolicyAndAutoRemove
|
|
|
|
}
|
2016-03-25 01:25:50 +00:00
|
|
|
attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
|
|
|
|
if attach {
|
2015-03-25 03:57:23 +00:00
|
|
|
var (
|
|
|
|
out, stderr io.Writer
|
|
|
|
in io.ReadCloser
|
|
|
|
)
|
|
|
|
if config.AttachStdin {
|
|
|
|
in = cli.in
|
|
|
|
}
|
|
|
|
if config.AttachStdout {
|
|
|
|
out = cli.out
|
|
|
|
}
|
|
|
|
if config.AttachStderr {
|
|
|
|
if config.Tty {
|
|
|
|
stderr = cli.out
|
|
|
|
} else {
|
|
|
|
stderr = cli.err
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 04:18:06 +00:00
|
|
|
|
2016-01-03 22:03:39 +00:00
|
|
|
if *flDetachKeys != "" {
|
|
|
|
cli.configFile.DetachKeys = *flDetachKeys
|
|
|
|
}
|
|
|
|
|
2015-12-06 04:18:06 +00:00
|
|
|
options := types.ContainerAttachOptions{
|
2016-04-13 08:33:46 +00:00
|
|
|
Stream: true,
|
|
|
|
Stdin: config.AttachStdin,
|
|
|
|
Stdout: config.AttachStdout,
|
|
|
|
Stderr: config.AttachStderr,
|
|
|
|
DetachKeys: cli.configFile.DetachKeys,
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-12-06 04:18:06 +00:00
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
resp, errAttach := cli.client.ContainerAttach(context.Background(), createResponse.ID, options)
|
2016-03-23 11:34:47 +00: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-25 03:57:23 +00:00
|
|
|
}
|
2016-04-27 08:48:21 +00:00
|
|
|
defer resp.Close()
|
|
|
|
|
2016-03-25 01:25:50 +00:00
|
|
|
ctx, cancelFun = context.WithCancel(context.Background())
|
2015-12-06 04:18:06 +00:00
|
|
|
errCh = promise.Go(func() error {
|
2016-03-23 11:34:47 +00:00
|
|
|
errHijack := cli.holdHijackedConnection(ctx, config.Tty, in, out, stderr, resp)
|
|
|
|
if errHijack == nil {
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
return errHijack
|
2015-12-06 04:18:06 +00:00
|
|
|
})
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:30:58 +00:00
|
|
|
if *flAutoRemove {
|
|
|
|
defer func() {
|
2016-03-23 11:34:47 +00:00
|
|
|
if err := cli.removeContainer(createResponse.ID, true, false, true); err != nil {
|
2016-02-01 20:30:58 +00:00
|
|
|
fmt.Fprintf(cli.err, "%v\n", err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2016-02-01 20:30:58 +00:00
|
|
|
}()
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
//start the container
|
2016-03-16 19:19:22 +00:00
|
|
|
if err := cli.client.ContainerStart(context.Background(), createResponse.ID); err != nil {
|
2016-03-25 01:25:50 +00: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
|
|
|
|
}
|
|
|
|
|
2015-07-29 12:21:16 +00:00
|
|
|
cmd.ReportError(err.Error(), false)
|
|
|
|
return runStartContainerErr(err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config.AttachStdin || config.AttachStdout || config.AttachStderr) && config.Tty && cli.isTerminalOut {
|
|
|
|
if err := cli.monitorTtySize(createResponse.ID, 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 errCh != nil {
|
|
|
|
if err := <-errCh; err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error hijack: %s", err)
|
2015-03-25 03:57:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detached mode: wait for the id to be displayed and return.
|
|
|
|
if !config.AttachStdout && !config.AttachStderr {
|
|
|
|
// Detached mode
|
2015-03-26 02:31:29 +00:00
|
|
|
<-waitDisplayID
|
2015-03-25 03:57:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var status int
|
|
|
|
|
|
|
|
// Attached mode
|
|
|
|
if *flAutoRemove {
|
|
|
|
// Autoremove: wait for the container to finish, retrieve
|
|
|
|
// the exit code and remove the container
|
2016-02-03 23:41:26 +00:00
|
|
|
if status, err = cli.client.ContainerWait(context.Background(), createResponse.ID); err != nil {
|
2015-07-29 12:21:16 +00:00
|
|
|
return runStartContainerErr(err)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No Autoremove: Simply retrieve the exit code
|
|
|
|
if !config.Tty {
|
|
|
|
// In non-TTY mode, we can't detach, so we must wait for container exit
|
2016-02-03 23:41:26 +00:00
|
|
|
if status, err = cli.client.ContainerWait(context.Background(), createResponse.ID); err != nil {
|
2015-03-25 03:57:23 +00: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
|
|
|
|
if _, status, err = getExitCode(cli, createResponse.ID); 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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|