start.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "runtime"
  5. "time"
  6. "github.com/containerd/containerd/log"
  7. "github.com/docker/docker/api/types"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/errdefs"
  11. "github.com/docker/docker/libcontainerd"
  12. "github.com/pkg/errors"
  13. )
  14. // ContainerStart starts a container.
  15. func (daemon *Daemon) ContainerStart(ctx context.Context, name string, hostConfig *containertypes.HostConfig, checkpoint string, checkpointDir string) error {
  16. daemonCfg := daemon.config()
  17. if checkpoint != "" && !daemonCfg.Experimental {
  18. return errdefs.InvalidParameter(errors.New("checkpoint is only supported in experimental mode"))
  19. }
  20. ctr, err := daemon.GetContainer(name)
  21. if err != nil {
  22. return err
  23. }
  24. validateState := func() error {
  25. ctr.Lock()
  26. defer ctr.Unlock()
  27. if ctr.Paused {
  28. return errdefs.Conflict(errors.New("cannot start a paused container, try unpause instead"))
  29. }
  30. if ctr.Running {
  31. return containerNotModifiedError{running: true}
  32. }
  33. if ctr.RemovalInProgress || ctr.Dead {
  34. return errdefs.Conflict(errors.New("container is marked for removal and cannot be started"))
  35. }
  36. return nil
  37. }
  38. if err := validateState(); err != nil {
  39. return err
  40. }
  41. // Windows does not have the backwards compatibility issue here.
  42. if runtime.GOOS != "windows" {
  43. // This is kept for backward compatibility - hostconfig should be passed when
  44. // creating a container, not during start.
  45. if hostConfig != nil {
  46. log.G(ctx).Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and has been removed in Docker 1.12")
  47. oldNetworkMode := ctr.HostConfig.NetworkMode
  48. if err := daemon.setSecurityOptions(&daemonCfg.Config, ctr, hostConfig); err != nil {
  49. return errdefs.InvalidParameter(err)
  50. }
  51. if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil {
  52. return errdefs.InvalidParameter(err)
  53. }
  54. if err := daemon.setHostConfig(ctr, hostConfig); err != nil {
  55. return errdefs.InvalidParameter(err)
  56. }
  57. newNetworkMode := ctr.HostConfig.NetworkMode
  58. if string(oldNetworkMode) != string(newNetworkMode) {
  59. // if user has change the network mode on starting, clean up the
  60. // old networks. It is a deprecated feature and has been removed in Docker 1.12
  61. ctr.NetworkSettings.Networks = nil
  62. }
  63. if err := ctr.CheckpointTo(daemon.containersReplica); err != nil {
  64. return errdefs.System(err)
  65. }
  66. ctr.InitDNSHostConfig()
  67. }
  68. } else {
  69. if hostConfig != nil {
  70. return errdefs.InvalidParameter(errors.New("Supplying a hostconfig on start is not supported. It should be supplied on create"))
  71. }
  72. }
  73. // check if hostConfig is in line with the current system settings.
  74. // It may happen cgroups are umounted or the like.
  75. if _, err = daemon.verifyContainerSettings(daemonCfg, ctr.HostConfig, nil, false); err != nil {
  76. return errdefs.InvalidParameter(err)
  77. }
  78. // Adapt for old containers in case we have updates in this function and
  79. // old containers never have chance to call the new function in create stage.
  80. if hostConfig != nil {
  81. if err := daemon.adaptContainerSettings(&daemonCfg.Config, ctr.HostConfig, false); err != nil {
  82. return errdefs.InvalidParameter(err)
  83. }
  84. }
  85. return daemon.containerStart(ctx, daemonCfg, ctr, checkpoint, checkpointDir, true)
  86. }
  87. // containerStart prepares the container to run by setting up everything the
  88. // container needs, such as storage and networking, as well as links
  89. // between containers. The container is left waiting for a signal to
  90. // begin running.
  91. func (daemon *Daemon) containerStart(ctx context.Context, daemonCfg *configStore, container *container.Container, checkpoint string, checkpointDir string, resetRestartManager bool) (retErr error) {
  92. start := time.Now()
  93. container.Lock()
  94. defer container.Unlock()
  95. if resetRestartManager && container.Running { // skip this check if already in restarting step and resetRestartManager==false
  96. return nil
  97. }
  98. if container.RemovalInProgress || container.Dead {
  99. return errdefs.Conflict(errors.New("container is marked for removal and cannot be started"))
  100. }
  101. if checkpointDir != "" {
  102. // TODO(mlaventure): how would we support that?
  103. return errdefs.Forbidden(errors.New("custom checkpointdir is not supported"))
  104. }
  105. // if we encounter an error during start we need to ensure that any other
  106. // setup has been cleaned up properly
  107. defer func() {
  108. if retErr != nil {
  109. container.SetError(retErr)
  110. // if no one else has set it, make sure we don't leave it at zero
  111. if container.ExitCode() == 0 {
  112. container.SetExitCode(exitUnknown)
  113. }
  114. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  115. log.G(ctx).Errorf("%s: failed saving state on start failure: %v", container.ID, err)
  116. }
  117. container.Reset(false)
  118. daemon.Cleanup(container)
  119. // if containers AutoRemove flag is set, remove it after clean up
  120. if container.HostConfig.AutoRemove {
  121. container.Unlock()
  122. if err := daemon.containerRm(&daemonCfg.Config, container.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
  123. log.G(ctx).Errorf("can't remove container %s: %v", container.ID, err)
  124. }
  125. container.Lock()
  126. }
  127. }
  128. }()
  129. if err := daemon.conditionalMountOnStart(container); err != nil {
  130. return err
  131. }
  132. if err := daemon.initializeNetworking(&daemonCfg.Config, container); err != nil {
  133. return err
  134. }
  135. spec, err := daemon.createSpec(ctx, daemonCfg, container)
  136. if err != nil {
  137. return errdefs.System(err)
  138. }
  139. if resetRestartManager {
  140. container.ResetRestartManager(true)
  141. container.HasBeenManuallyStopped = false
  142. }
  143. if err := daemon.saveAppArmorConfig(container); err != nil {
  144. return err
  145. }
  146. if checkpoint != "" {
  147. checkpointDir, err = getCheckpointDir(checkpointDir, checkpoint, container.Name, container.ID, container.CheckpointDir(), false)
  148. if err != nil {
  149. return err
  150. }
  151. }
  152. shim, createOptions, err := daemon.getLibcontainerdCreateOptions(daemonCfg, container)
  153. if err != nil {
  154. return err
  155. }
  156. ctr, err := libcontainerd.ReplaceContainer(ctx, daemon.containerd, container.ID, spec, shim, createOptions)
  157. if err != nil {
  158. return setExitCodeFromError(container.SetExitCode, err)
  159. }
  160. // TODO(mlaventure): we need to specify checkpoint options here
  161. tsk, err := ctr.Start(context.TODO(), // Passing ctx to ctr.Start caused integration tests to be stuck in the cleanup phase
  162. checkpointDir, container.StreamConfig.Stdin() != nil || container.Config.Tty,
  163. container.InitializeStdio)
  164. if err != nil {
  165. if err := ctr.Delete(context.Background()); err != nil {
  166. log.G(ctx).WithError(err).WithField("container", container.ID).
  167. Error("failed to delete failed start container")
  168. }
  169. return setExitCodeFromError(container.SetExitCode, err)
  170. }
  171. container.HasBeenManuallyRestarted = false
  172. container.SetRunning(ctr, tsk, true)
  173. container.HasBeenStartedBefore = true
  174. daemon.setStateCounter(container)
  175. daemon.initHealthMonitor(container)
  176. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  177. log.G(ctx).WithError(err).WithField("container", container.ID).
  178. Errorf("failed to store container")
  179. }
  180. daemon.LogContainerEvent(container, "start")
  181. containerActions.WithValues("start").UpdateSince(start)
  182. return nil
  183. }
  184. // Cleanup releases any network resources allocated to the container along with any rules
  185. // around how containers are linked together. It also unmounts the container's root filesystem.
  186. func (daemon *Daemon) Cleanup(container *container.Container) {
  187. // Microsoft HCS containers get in a bad state if host resources are
  188. // released while the container still exists.
  189. if ctr, ok := container.C8dContainer(); ok {
  190. if err := ctr.Delete(context.Background()); err != nil {
  191. log.G(context.TODO()).Errorf("%s cleanup: failed to delete container from containerd: %v", container.ID, err)
  192. }
  193. }
  194. daemon.releaseNetwork(container)
  195. if err := container.UnmountIpcMount(); err != nil {
  196. log.G(context.TODO()).Warnf("%s cleanup: failed to unmount IPC: %s", container.ID, err)
  197. }
  198. if err := daemon.conditionalUnmountOnCleanup(container); err != nil {
  199. // FIXME: remove once reference counting for graphdrivers has been refactored
  200. // Ensure that all the mounts are gone
  201. if mountid, err := daemon.imageService.GetLayerMountID(container.ID); err == nil {
  202. daemon.cleanupMountsByID(mountid)
  203. }
  204. }
  205. if err := container.UnmountSecrets(); err != nil {
  206. log.G(context.TODO()).Warnf("%s cleanup: failed to unmount secrets: %s", container.ID, err)
  207. }
  208. if err := recursiveUnmount(container.Root); err != nil {
  209. log.G(context.TODO()).WithError(err).WithField("container", container.ID).Warn("Error while cleaning up container resource mounts.")
  210. }
  211. for _, eConfig := range container.ExecCommands.Commands() {
  212. daemon.unregisterExecCommand(container, eConfig)
  213. }
  214. if container.BaseFS != "" {
  215. if err := container.UnmountVolumes(daemon.LogVolumeEvent); err != nil {
  216. log.G(context.TODO()).Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
  217. }
  218. }
  219. container.CancelAttachContext()
  220. }