start.go 10 KB

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