errors.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "fmt"
  4. "strings"
  5. "syscall"
  6. "github.com/docker/docker/errdefs"
  7. "github.com/pkg/errors"
  8. "google.golang.org/grpc/status"
  9. )
  10. func errNotRunning(id string) error {
  11. return errdefs.Conflict(errors.Errorf("Container %s is not running", id))
  12. }
  13. func containerNotFound(id string) error {
  14. return objNotFoundError{"container", id}
  15. }
  16. type objNotFoundError struct {
  17. object string
  18. id string
  19. }
  20. func (e objNotFoundError) Error() string {
  21. return "No such " + e.object + ": " + e.id
  22. }
  23. func (e objNotFoundError) NotFound() {}
  24. func errContainerIsRestarting(containerID string) error {
  25. cause := errors.Errorf("Container %s is restarting, wait until the container is running", containerID)
  26. return errdefs.Conflict(cause)
  27. }
  28. func errExecNotFound(id string) error {
  29. return objNotFoundError{"exec instance", id}
  30. }
  31. func errExecPaused(id string) error {
  32. cause := errors.Errorf("Container %s is paused, unpause the container before exec", id)
  33. return errdefs.Conflict(cause)
  34. }
  35. func errNotPaused(id string) error {
  36. cause := errors.Errorf("Container %s is already paused", id)
  37. return errdefs.Conflict(cause)
  38. }
  39. type nameConflictError struct {
  40. id string
  41. name string
  42. }
  43. func (e nameConflictError) Error() string {
  44. return fmt.Sprintf("Conflict. The container name %q is already in use by container %q. You have to remove (or rename) that container to be able to reuse that name.", e.name, e.id)
  45. }
  46. func (nameConflictError) Conflict() {}
  47. type containerNotModifiedError struct {
  48. running bool
  49. }
  50. func (e containerNotModifiedError) Error() string {
  51. if e.running {
  52. return "Container is already started"
  53. }
  54. return "Container is already stopped"
  55. }
  56. func (e containerNotModifiedError) NotModified() {}
  57. type invalidIdentifier string
  58. func (e invalidIdentifier) Error() string {
  59. return fmt.Sprintf("invalid name or ID supplied: %q", string(e))
  60. }
  61. func (invalidIdentifier) InvalidParameter() {}
  62. type incompatibleDeviceRequest struct {
  63. driver string
  64. caps [][]string
  65. }
  66. func (i incompatibleDeviceRequest) Error() string {
  67. return fmt.Sprintf("could not select device driver %q with capabilities: %v", i.driver, i.caps)
  68. }
  69. func (incompatibleDeviceRequest) InvalidParameter() {}
  70. type duplicateMountPointError string
  71. func (e duplicateMountPointError) Error() string {
  72. return "Duplicate mount point: " + string(e)
  73. }
  74. func (duplicateMountPointError) InvalidParameter() {}
  75. type containerFileNotFound struct {
  76. file string
  77. container string
  78. }
  79. func (e containerFileNotFound) Error() string {
  80. return "Could not find the file " + e.file + " in container " + e.container
  81. }
  82. func (containerFileNotFound) NotFound() {}
  83. type startInvalidConfigError string
  84. func (e startInvalidConfigError) Error() string {
  85. return string(e)
  86. }
  87. func (e startInvalidConfigError) InvalidParameter() {} // Is this right???
  88. // exitStatus is the exit-code as set by setExitCodeFromError
  89. type exitStatus = int
  90. const (
  91. exitEaccess exitStatus = 126 // container cmd can't be invoked (permission denied)
  92. exitCmdNotFound exitStatus = 127 // container cmd not found/does not exist or invalid bind-mount
  93. exitUnknown exitStatus = 128 // unknown error
  94. )
  95. // setExitCodeFromError converts the error returned by containerd
  96. // when starting a container, and applies the corresponding exitStatus to the
  97. // container. It returns an errdefs error (either errdefs.ErrInvalidParameter
  98. // or errdefs.ErrUnknown).
  99. func setExitCodeFromError(setExitCode func(exitStatus), err error) error {
  100. if err == nil {
  101. return nil
  102. }
  103. errDesc := status.Convert(err).Message()
  104. contains := func(s1, s2 string) bool {
  105. return strings.Contains(strings.ToLower(s1), s2)
  106. }
  107. // set to 126 for container cmd can't be invoked errors
  108. if contains(errDesc, syscall.EACCES.Error()) {
  109. setExitCode(exitEaccess)
  110. return startInvalidConfigError(errDesc)
  111. }
  112. // attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
  113. if contains(errDesc, syscall.ENOTDIR.Error()) {
  114. errDesc += ": Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type"
  115. setExitCode(exitCmdNotFound)
  116. return startInvalidConfigError(errDesc)
  117. }
  118. // if we receive an internal error from the initial start of a container then lets
  119. // return it instead of entering the restart loop
  120. // set to 127 for container cmd not found/does not exist.
  121. if isInvalidCommand(errDesc) {
  122. setExitCode(exitCmdNotFound)
  123. return startInvalidConfigError(errDesc)
  124. }
  125. // TODO: it would be nice to get some better errors from containerd so we can return better errors here
  126. setExitCode(exitUnknown)
  127. return errdefs.Unknown(errors.New(errDesc))
  128. }
  129. // isInvalidCommand tries to detect if the reason the container failed to start
  130. // was due to an invalid command for the container (command not found, or not
  131. // a valid executable).
  132. func isInvalidCommand(errMessage string) bool {
  133. errMessage = strings.ToLower(errMessage)
  134. errMessages := []string{
  135. "executable file not found",
  136. "no such file or directory",
  137. "system cannot find the file specified",
  138. "failed to run runc create/exec call",
  139. }
  140. for _, msg := range errMessages {
  141. if strings.Contains(errMessage, msg) {
  142. return true
  143. }
  144. }
  145. return false
  146. }