start.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package daemon
  2. import (
  3. "fmt"
  4. "net/http"
  5. "runtime"
  6. "strings"
  7. "syscall"
  8. "google.golang.org/grpc"
  9. "github.com/Sirupsen/logrus"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/errors"
  12. "github.com/docker/docker/libcontainerd"
  13. "github.com/docker/docker/runconfig"
  14. containertypes "github.com/docker/engine-api/types/container"
  15. )
  16. // ContainerStart starts a container.
  17. func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, validateHostname bool) error {
  18. container, err := daemon.GetContainer(name)
  19. if err != nil {
  20. return err
  21. }
  22. if container.IsPaused() {
  23. return fmt.Errorf("Cannot start a paused container, try unpause instead.")
  24. }
  25. if container.IsRunning() {
  26. err := fmt.Errorf("Container already started")
  27. return errors.NewErrorWithStatusCode(err, http.StatusNotModified)
  28. }
  29. // Windows does not have the backwards compatibility issue here.
  30. if runtime.GOOS != "windows" {
  31. // This is kept for backward compatibility - hostconfig should be passed when
  32. // creating a container, not during start.
  33. if hostConfig != nil {
  34. logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12")
  35. oldNetworkMode := container.HostConfig.NetworkMode
  36. if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
  37. return err
  38. }
  39. if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil {
  40. return err
  41. }
  42. if err := daemon.setHostConfig(container, hostConfig); err != nil {
  43. return err
  44. }
  45. newNetworkMode := container.HostConfig.NetworkMode
  46. if string(oldNetworkMode) != string(newNetworkMode) {
  47. // if user has change the network mode on starting, clean up the
  48. // old networks. It is a deprecated feature and will be removed in Docker 1.12
  49. container.NetworkSettings.Networks = nil
  50. if err := container.ToDisk(); err != nil {
  51. return err
  52. }
  53. }
  54. container.InitDNSHostConfig()
  55. }
  56. } else {
  57. if hostConfig != nil {
  58. return fmt.Errorf("Supplying a hostconfig on start is not supported. It should be supplied on create")
  59. }
  60. }
  61. // check if hostConfig is in line with the current system settings.
  62. // It may happen cgroups are umounted or the like.
  63. if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false, validateHostname); err != nil {
  64. return err
  65. }
  66. // Adapt for old containers in case we have updates in this function and
  67. // old containers never have chance to call the new function in create stage.
  68. if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil {
  69. return err
  70. }
  71. return daemon.containerStart(container)
  72. }
  73. // Start starts a container
  74. func (daemon *Daemon) Start(container *container.Container) error {
  75. return daemon.containerStart(container)
  76. }
  77. // containerStart prepares the container to run by setting up everything the
  78. // container needs, such as storage and networking, as well as links
  79. // between containers. The container is left waiting for a signal to
  80. // begin running.
  81. func (daemon *Daemon) containerStart(container *container.Container) (err error) {
  82. container.Lock()
  83. defer container.Unlock()
  84. if container.Running {
  85. return nil
  86. }
  87. if container.RemovalInProgress || container.Dead {
  88. return fmt.Errorf("Container is marked for removal and cannot be started.")
  89. }
  90. // if we encounter an error during start we need to ensure that any other
  91. // setup has been cleaned up properly
  92. defer func() {
  93. if err != nil {
  94. container.SetError(err)
  95. // if no one else has set it, make sure we don't leave it at zero
  96. if container.ExitCode() == 0 {
  97. container.SetExitCode(128)
  98. }
  99. container.ToDisk()
  100. daemon.Cleanup(container)
  101. }
  102. }()
  103. if err := daemon.conditionalMountOnStart(container); err != nil {
  104. return err
  105. }
  106. // Make sure NetworkMode has an acceptable value. We do this to ensure
  107. // backwards API compatibility.
  108. container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig)
  109. if err := daemon.initializeNetworking(container); err != nil {
  110. return err
  111. }
  112. spec, err := daemon.createSpec(container)
  113. if err != nil {
  114. return err
  115. }
  116. createOptions := []libcontainerd.CreateOption{libcontainerd.WithRestartManager(container.RestartManager(true))}
  117. copts, err := daemon.getLibcontainerdCreateOptions(container)
  118. if err != nil {
  119. return err
  120. }
  121. if copts != nil {
  122. createOptions = append(createOptions, *copts...)
  123. }
  124. if err := daemon.containerd.Create(container.ID, *spec, createOptions...); err != nil {
  125. errDesc := grpc.ErrorDesc(err)
  126. logrus.Errorf("Create container failed with error: %s", errDesc)
  127. // if we receive an internal error from the initial start of a container then lets
  128. // return it instead of entering the restart loop
  129. // set to 127 for container cmd not found/does not exist)
  130. if strings.Contains(errDesc, container.Path) &&
  131. (strings.Contains(errDesc, "executable file not found") ||
  132. strings.Contains(errDesc, "no such file or directory") ||
  133. strings.Contains(errDesc, "system cannot find the file specified")) {
  134. container.SetExitCode(127)
  135. }
  136. // set to 126 for container cmd can't be invoked errors
  137. if strings.Contains(errDesc, syscall.EACCES.Error()) {
  138. container.SetExitCode(126)
  139. }
  140. // attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
  141. if strings.Contains(errDesc, syscall.ENOTDIR.Error()) {
  142. 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"
  143. container.SetExitCode(127)
  144. }
  145. container.Reset(false)
  146. return fmt.Errorf("%s", errDesc)
  147. }
  148. return nil
  149. }
  150. // Cleanup releases any network resources allocated to the container along with any rules
  151. // around how containers are linked together. It also unmounts the container's root filesystem.
  152. func (daemon *Daemon) Cleanup(container *container.Container) {
  153. daemon.releaseNetwork(container)
  154. container.UnmountIpcMounts(detachMounted)
  155. if err := daemon.conditionalUnmountOnCleanup(container); err != nil {
  156. // FIXME: remove once reference counting for graphdrivers has been refactored
  157. // Ensure that all the mounts are gone
  158. if mountid, err := daemon.layerStore.GetMountID(container.ID); err == nil {
  159. daemon.cleanupMountsByID(mountid)
  160. }
  161. }
  162. for _, eConfig := range container.ExecCommands.Commands() {
  163. daemon.unregisterExecCommand(container, eConfig)
  164. }
  165. if container.BaseFS != "" {
  166. if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil {
  167. logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
  168. }
  169. }
  170. container.CancelAttachContext()
  171. }