start.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package daemon
  2. import (
  3. "runtime"
  4. "github.com/Sirupsen/logrus"
  5. "github.com/docker/docker/container"
  6. derr "github.com/docker/docker/errors"
  7. "github.com/docker/docker/runconfig"
  8. containertypes "github.com/docker/engine-api/types/container"
  9. )
  10. // ContainerStart starts a container.
  11. func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig) error {
  12. container, err := daemon.GetContainer(name)
  13. if err != nil {
  14. return err
  15. }
  16. if container.IsPaused() {
  17. return derr.ErrorCodeStartPaused
  18. }
  19. if container.IsRunning() {
  20. return derr.ErrorCodeAlreadyStarted
  21. }
  22. // Windows does not have the backwards compatibility issue here.
  23. if runtime.GOOS != "windows" {
  24. // This is kept for backward compatibility - hostconfig should be passed when
  25. // creating a container, not during start.
  26. if hostConfig != nil {
  27. logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12")
  28. oldNetworkMode := container.HostConfig.NetworkMode
  29. if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
  30. return err
  31. }
  32. if err := daemon.setHostConfig(container, hostConfig); err != nil {
  33. return err
  34. }
  35. newNetworkMode := container.HostConfig.NetworkMode
  36. if string(oldNetworkMode) != string(newNetworkMode) {
  37. // if user has change the network mode on starting, clean up the
  38. // old networks. It is a deprecated feature and will be removed in Docker 1.12
  39. container.NetworkSettings.Networks = nil
  40. if err := container.ToDisk(); err != nil {
  41. return err
  42. }
  43. }
  44. container.InitDNSHostConfig()
  45. }
  46. } else {
  47. if hostConfig != nil {
  48. return derr.ErrorCodeHostConfigStart
  49. }
  50. }
  51. // check if hostConfig is in line with the current system settings.
  52. // It may happen cgroups are umounted or the like.
  53. if _, err = daemon.verifyContainerSettings(container.HostConfig, nil); err != nil {
  54. return err
  55. }
  56. // Adapt for old containers in case we have updates in this function and
  57. // old containers never have chance to call the new function in create stage.
  58. if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil {
  59. return err
  60. }
  61. return daemon.containerStart(container)
  62. }
  63. // Start starts a container
  64. func (daemon *Daemon) Start(container *container.Container) error {
  65. return daemon.containerStart(container)
  66. }
  67. // containerStart prepares the container to run by setting up everything the
  68. // container needs, such as storage and networking, as well as links
  69. // between containers. The container is left waiting for a signal to
  70. // begin running.
  71. func (daemon *Daemon) containerStart(container *container.Container) (err error) {
  72. container.Lock()
  73. defer container.Unlock()
  74. if container.Running {
  75. return nil
  76. }
  77. if container.RemovalInProgress || container.Dead {
  78. return derr.ErrorCodeContainerBeingRemoved
  79. }
  80. // if we encounter an error during start we need to ensure that any other
  81. // setup has been cleaned up properly
  82. defer func() {
  83. if err != nil {
  84. container.SetError(err)
  85. // if no one else has set it, make sure we don't leave it at zero
  86. if container.ExitCode == 0 {
  87. container.ExitCode = 128
  88. }
  89. container.ToDisk()
  90. daemon.Cleanup(container)
  91. daemon.LogContainerEvent(container, "die")
  92. }
  93. }()
  94. if err := daemon.conditionalMountOnStart(container); err != nil {
  95. return err
  96. }
  97. // Make sure NetworkMode has an acceptable value. We do this to ensure
  98. // backwards API compatibility.
  99. container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig)
  100. if err := daemon.initializeNetworking(container); err != nil {
  101. return err
  102. }
  103. linkedEnv, err := daemon.setupLinkedContainers(container)
  104. if err != nil {
  105. return err
  106. }
  107. if err := container.SetupWorkingDirectory(); err != nil {
  108. return err
  109. }
  110. env := container.CreateDaemonEnvironment(linkedEnv)
  111. if err := daemon.populateCommand(container, env); err != nil {
  112. return err
  113. }
  114. if !container.HostConfig.IpcMode.IsContainer() && !container.HostConfig.IpcMode.IsHost() {
  115. if err := daemon.setupIpcDirs(container); err != nil {
  116. return err
  117. }
  118. }
  119. mounts, err := daemon.setupMounts(container)
  120. if err != nil {
  121. return err
  122. }
  123. mounts = append(mounts, container.IpcMounts()...)
  124. mounts = append(mounts, container.TmpfsMounts()...)
  125. container.Command.Mounts = mounts
  126. if err := daemon.waitForStart(container); err != nil {
  127. return err
  128. }
  129. container.HasBeenStartedBefore = true
  130. return nil
  131. }
  132. func (daemon *Daemon) waitForStart(container *container.Container) error {
  133. return container.StartMonitor(daemon, container.HostConfig.RestartPolicy)
  134. }
  135. // Cleanup releases any network resources allocated to the container along with any rules
  136. // around how containers are linked together. It also unmounts the container's root filesystem.
  137. func (daemon *Daemon) Cleanup(container *container.Container) {
  138. daemon.releaseNetwork(container)
  139. container.UnmountIpcMounts(detachMounted)
  140. daemon.conditionalUnmountOnCleanup(container)
  141. for _, eConfig := range container.ExecCommands.Commands() {
  142. daemon.unregisterExecCommand(container, eConfig)
  143. }
  144. if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil {
  145. logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
  146. }
  147. }