浏览代码

Merge pull request #23129 from WeiZhang555/print-detailed-error

Print original error for `start`
Vincent Demeester 9 年之前
父节点
当前提交
9d449d89f7
共有 2 个文件被更改,包括 18 次插入15 次删除
  1. 9 8
      api/client/container/run.go
  2. 9 7
      daemon/start.go

+ 9 - 8
api/client/container/run.go

@@ -7,6 +7,7 @@ import (
 	"os"
 	"runtime"
 	"strings"
+	"syscall"
 
 	"golang.org/x/net/context"
 
@@ -315,18 +316,18 @@ func reportError(stderr io.Writer, name string, str string, withHelp bool) {
 	fmt.Fprintf(stderr, "%s: %s.\n", os.Args[0], str)
 }
 
-// if container start fails with 'command not found' error, return 127
-// if container start fails with 'command cannot be invoked' error, return 126
+// if container start fails with 'not found'/'no such' error, return 127
+// if container start fails with 'permission denied' error, return 126
 // 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}
-	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}
-		}
+	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}
 	}
 
 	return statusError

+ 9 - 7
daemon/start.go

@@ -7,6 +7,8 @@ import (
 	"strings"
 	"syscall"
 
+	"google.golang.org/grpc"
+
 	"github.com/Sirupsen/logrus"
 	"github.com/docker/docker/container"
 	"github.com/docker/docker/errors"
@@ -131,24 +133,24 @@ func (daemon *Daemon) containerStart(container *container.Container) (err error)
 	}
 
 	if err := daemon.containerd.Create(container.ID, *spec, libcontainerd.WithRestartManager(container.RestartManager(true))); err != nil {
+		errDesc := grpc.ErrorDesc(err)
+		logrus.Errorf("Create container failed with error: %s", errDesc)
 		// if we receive an internal error from the initial start of a container then lets
 		// return it instead of entering the restart loop
 		// set to 127 for container cmd not found/does not exist)
-		if strings.Contains(err.Error(), "executable file not found") ||
-			strings.Contains(err.Error(), "no such file or directory") ||
-			strings.Contains(err.Error(), "system cannot find the file specified") {
+		if strings.Contains(errDesc, "executable file not found") ||
+			strings.Contains(errDesc, "no such file or directory") ||
+			strings.Contains(errDesc, "system cannot find the file specified") {
 			container.ExitCode = 127
-			err = fmt.Errorf("Container command '%s' not found or does not exist", container.Path)
 		}
 		// set to 126 for container cmd can't be invoked errors
-		if strings.Contains(err.Error(), syscall.EACCES.Error()) {
+		if strings.Contains(errDesc, syscall.EACCES.Error()) {
 			container.ExitCode = 126
-			err = fmt.Errorf("Container command '%s' could not be invoked", container.Path)
 		}
 
 		container.Reset(false)
 
-		return err
+		return fmt.Errorf("%s", errDesc)
 	}
 
 	return nil