start.go 6.1 KB

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