start.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package daemon
  2. import (
  3. "runtime"
  4. "time"
  5. "github.com/docker/docker/api/types"
  6. containertypes "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/container"
  8. "github.com/pkg/errors"
  9. "github.com/sirupsen/logrus"
  10. )
  11. // ContainerStart starts a container.
  12. func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig, checkpoint string, checkpointDir string) error {
  13. if checkpoint != "" && !daemon.HasExperimental() {
  14. return validationError{errors.New("checkpoint is only supported in experimental mode")}
  15. }
  16. container, err := daemon.GetContainer(name)
  17. if err != nil {
  18. return err
  19. }
  20. validateState := func() error {
  21. container.Lock()
  22. defer container.Unlock()
  23. if container.Paused {
  24. return stateConflictError{errors.New("cannot start a paused container, try unpause instead")}
  25. }
  26. if container.Running {
  27. return containerNotModifiedError{running: true}
  28. }
  29. if container.RemovalInProgress || container.Dead {
  30. return stateConflictError{errors.New("container is marked for removal and cannot be started")}
  31. }
  32. return nil
  33. }
  34. if err := validateState(); err != nil {
  35. return err
  36. }
  37. // Windows does not have the backwards compatibility issue here.
  38. if runtime.GOOS != "windows" {
  39. // This is kept for backward compatibility - hostconfig should be passed when
  40. // creating a container, not during start.
  41. if hostConfig != nil {
  42. logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and has been removed in Docker 1.12")
  43. oldNetworkMode := container.HostConfig.NetworkMode
  44. if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
  45. return validationError{err}
  46. }
  47. if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil {
  48. return validationError{err}
  49. }
  50. if err := daemon.setHostConfig(container, hostConfig); err != nil {
  51. return validationError{err}
  52. }
  53. newNetworkMode := container.HostConfig.NetworkMode
  54. if string(oldNetworkMode) != string(newNetworkMode) {
  55. // if user has change the network mode on starting, clean up the
  56. // old networks. It is a deprecated feature and has been removed in Docker 1.12
  57. container.NetworkSettings.Networks = nil
  58. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  59. return systemError{err}
  60. }
  61. }
  62. container.InitDNSHostConfig()
  63. }
  64. } else {
  65. if hostConfig != nil {
  66. return validationError{errors.New("Supplying a hostconfig on start is not supported. It should be supplied on create")}
  67. }
  68. }
  69. // check if hostConfig is in line with the current system settings.
  70. // It may happen cgroups are umounted or the like.
  71. if _, err = daemon.verifyContainerSettings(container.Platform, container.HostConfig, nil, false); err != nil {
  72. return validationError{err}
  73. }
  74. // Adapt for old containers in case we have updates in this function and
  75. // old containers never have chance to call the new function in create stage.
  76. if hostConfig != nil {
  77. if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil {
  78. return validationError{err}
  79. }
  80. }
  81. if err := daemon.containerStart(container, checkpoint, checkpointDir, true); err != nil {
  82. return err
  83. }
  84. return nil
  85. }
  86. // containerStart prepares the container to run by setting up everything the
  87. // container needs, such as storage and networking, as well as links
  88. // between containers. The container is left waiting for a signal to
  89. // begin running.
  90. func (daemon *Daemon) containerStart(container *container.Container, checkpoint string, checkpointDir string, resetRestartManager bool) (err error) {
  91. start := time.Now()
  92. container.Lock()
  93. defer container.Unlock()
  94. if resetRestartManager && container.Running { // skip this check if already in restarting step and resetRestartManager==false
  95. return nil
  96. }
  97. if container.RemovalInProgress || container.Dead {
  98. return stateConflictError{errors.New("container is marked for removal and cannot be started")}
  99. }
  100. // if we encounter an error during start we need to ensure that any other
  101. // setup has been cleaned up properly
  102. defer func() {
  103. if err != nil {
  104. container.SetError(err)
  105. // if no one else has set it, make sure we don't leave it at zero
  106. if container.ExitCode() == 0 {
  107. container.SetExitCode(128)
  108. }
  109. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  110. logrus.Errorf("%s: failed saving state on start failure: %v", container.ID, err)
  111. }
  112. container.Reset(false)
  113. daemon.Cleanup(container)
  114. // if containers AutoRemove flag is set, remove it after clean up
  115. if container.HostConfig.AutoRemove {
  116. container.Unlock()
  117. if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
  118. logrus.Errorf("can't remove container %s: %v", container.ID, err)
  119. }
  120. container.Lock()
  121. }
  122. }
  123. }()
  124. if err := daemon.conditionalMountOnStart(container); err != nil {
  125. return err
  126. }
  127. if err := daemon.initializeNetworking(container); err != nil {
  128. return err
  129. }
  130. spec, err := daemon.createSpec(container)
  131. if err != nil {
  132. return systemError{err}
  133. }
  134. createOptions, err := daemon.getLibcontainerdCreateOptions(container)
  135. if err != nil {
  136. return err
  137. }
  138. if resetRestartManager {
  139. container.ResetRestartManager(true)
  140. }
  141. if checkpointDir == "" {
  142. checkpointDir = container.CheckpointDir()
  143. }
  144. if daemon.saveApparmorConfig(container); err != nil {
  145. return err
  146. }
  147. if err := daemon.containerd.Create(container.ID, checkpoint, checkpointDir, *spec, container.InitializeStdio, createOptions...); err != nil {
  148. return translateContainerdStartErr(container.Path, container.SetExitCode, err)
  149. }
  150. containerActions.WithValues("start").UpdateSince(start)
  151. return nil
  152. }
  153. // Cleanup releases any network resources allocated to the container along with any rules
  154. // around how containers are linked together. It also unmounts the container's root filesystem.
  155. func (daemon *Daemon) Cleanup(container *container.Container) {
  156. daemon.releaseNetwork(container)
  157. if err := container.UnmountIpcMount(detachMounted); err != nil {
  158. logrus.Warnf("%s cleanup: failed to unmount IPC: %s", container.ID, err)
  159. }
  160. if err := daemon.conditionalUnmountOnCleanup(container); err != nil {
  161. // FIXME: remove once reference counting for graphdrivers has been refactored
  162. // Ensure that all the mounts are gone
  163. if mountid, err := daemon.stores[container.Platform].layerStore.GetMountID(container.ID); err == nil {
  164. daemon.cleanupMountsByID(mountid)
  165. }
  166. }
  167. if err := container.UnmountSecrets(); err != nil {
  168. logrus.Warnf("%s cleanup: failed to unmount secrets: %s", container.ID, err)
  169. }
  170. for _, eConfig := range container.ExecCommands.Commands() {
  171. daemon.unregisterExecCommand(container, eConfig)
  172. }
  173. if container.BaseFS != nil && container.BaseFS.Path() != "" {
  174. if err := container.UnmountVolumes(daemon.LogVolumeEvent); err != nil {
  175. logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
  176. }
  177. }
  178. container.CancelAttachContext()
  179. }