2014-08-01 00:46:18 +04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-07-17 01:33:13 +03:00
|
|
|
"runtime"
|
2015-03-25 10:44:12 +03:00
|
|
|
|
2015-11-03 20:43:36 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-18 21:36:17 +03:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 22:55:17 +03:00
|
|
|
"github.com/docker/docker/container"
|
2015-09-17 21:54:14 +03:00
|
|
|
derr "github.com/docker/docker/errors"
|
2014-08-01 00:46:18 +04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// ContainerStart starts a container.
|
2015-12-18 21:36:17 +03:00
|
|
|
func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig) error {
|
2015-12-11 20:39:28 +03:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-17 02:06:35 +03:00
|
|
|
if err != nil {
|
2015-03-25 10:44:12 +03:00
|
|
|
return err
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
if container.IsPaused() {
|
2015-09-16 21:56:26 +03:00
|
|
|
return derr.ErrorCodeStartPaused
|
2015-01-15 03:44:53 +03:00
|
|
|
}
|
|
|
|
|
2014-08-31 19:20:35 +04:00
|
|
|
if container.IsRunning() {
|
2015-09-16 21:56:26 +03:00
|
|
|
return derr.ErrorCodeAlreadyStarted
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
|
|
|
|
2015-08-08 01:24:18 +03:00
|
|
|
// Windows does not have the backwards compatibility issue here.
|
2015-07-17 01:33:13 +03:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// This is kept for backward compatibility - hostconfig should be passed when
|
|
|
|
// creating a container, not during start.
|
|
|
|
if hostConfig != nil {
|
2015-12-01 01:44:34 +03:00
|
|
|
logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12")
|
2015-12-21 22:23:20 +03:00
|
|
|
if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-29 20:51:40 +03:00
|
|
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
2015-07-17 01:33:13 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
container.InitDNSHostConfig()
|
2015-07-17 01:33:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hostConfig != nil {
|
2015-09-16 21:56:26 +03:00
|
|
|
return derr.ErrorCodeHostConfigStart
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:05:21 +03:00
|
|
|
|
2015-08-06 14:55:56 +03:00
|
|
|
// check if hostConfig is in line with the current system settings.
|
|
|
|
// It may happen cgroups are umounted or the like.
|
2015-11-12 22:55:17 +03:00
|
|
|
if _, err = daemon.verifyContainerSettings(container.HostConfig, nil); err != nil {
|
2015-08-06 14:55:56 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-12-15 03:42:26 +03:00
|
|
|
// Adapt for old containers in case we have updates in this function and
|
|
|
|
// old containers never have chance to call the new function in create stage.
|
|
|
|
if err := daemon.adaptContainerSettings(container.HostConfig, false); err != nil {
|
|
|
|
return err
|
2015-08-06 14:55:56 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
return daemon.containerStart(container)
|
2014-08-01 00:46:18 +04:00
|
|
|
}
|
2015-11-03 04:06:09 +03:00
|
|
|
|
|
|
|
// Start starts a container
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) Start(container *container.Container) error {
|
2015-11-03 04:06:09 +03:00
|
|
|
return daemon.containerStart(container)
|
|
|
|
}
|
|
|
|
|
|
|
|
// containerStart prepares the container to run by setting up everything the
|
|
|
|
// container needs, such as storage and networking, as well as links
|
|
|
|
// between containers. The container is left waiting for a signal to
|
|
|
|
// begin running.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) containerStart(container *container.Container) (err error) {
|
2015-11-03 04:06:09 +03:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
if container.Running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
if container.RemovalInProgress || container.Dead {
|
2015-11-03 04:06:09 +03:00
|
|
|
return derr.ErrorCodeContainerBeingRemoved
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we encounter an error during start we need to ensure that any other
|
|
|
|
// setup has been cleaned up properly
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-11-12 22:55:17 +03:00
|
|
|
container.SetError(err)
|
2015-11-03 04:06:09 +03:00
|
|
|
// if no one else has set it, make sure we don't leave it at zero
|
|
|
|
if container.ExitCode == 0 {
|
|
|
|
container.ExitCode = 128
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
container.ToDisk()
|
2015-11-03 20:43:36 +03:00
|
|
|
daemon.Cleanup(container)
|
2015-11-03 20:33:13 +03:00
|
|
|
daemon.LogContainerEvent(container, "die")
|
2015-11-03 04:06:09 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := daemon.conditionalMountOnStart(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure NetworkMode has an acceptable value. We do this to ensure
|
|
|
|
// backwards API compatibility.
|
2015-11-12 22:55:17 +03:00
|
|
|
container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig)
|
2015-11-03 04:06:09 +03:00
|
|
|
|
2015-11-03 21:25:09 +03:00
|
|
|
if err := daemon.initializeNetworking(container); err != nil {
|
2015-11-03 04:06:09 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-11-03 21:25:09 +03:00
|
|
|
linkedEnv, err := daemon.setupLinkedContainers(container)
|
2015-11-03 04:06:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
if err := container.SetupWorkingDirectory(); err != nil {
|
2015-11-03 04:06:09 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
env := container.CreateDaemonEnvironment(linkedEnv)
|
2015-11-03 21:25:09 +03:00
|
|
|
if err := daemon.populateCommand(container, env); err != nil {
|
2015-11-03 04:06:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
if !container.HostConfig.IpcMode.IsContainer() && !container.HostConfig.IpcMode.IsHost() {
|
2015-11-03 21:25:09 +03:00
|
|
|
if err := daemon.setupIpcDirs(container); err != nil {
|
2015-11-03 04:06:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:25:22 +03:00
|
|
|
mounts, err := daemon.setupMounts(container)
|
2015-11-03 04:06:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
mounts = append(mounts, container.IpcMounts()...)
|
|
|
|
mounts = append(mounts, container.TmpfsMounts()...)
|
2015-11-03 04:06:09 +03:00
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
container.Command.Mounts = mounts
|
2015-11-14 22:06:19 +03:00
|
|
|
if err := daemon.waitForStart(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.HasBeenStartedBefore = true
|
|
|
|
return nil
|
2015-11-03 20:33:13 +03:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) waitForStart(container *container.Container) error {
|
|
|
|
return container.StartMonitor(daemon, container.HostConfig.RestartPolicy)
|
2015-11-03 04:06:09 +03:00
|
|
|
}
|
2015-11-03 20:43:36 +03:00
|
|
|
|
|
|
|
// Cleanup releases any network resources allocated to the container along with any rules
|
|
|
|
// around how containers are linked together. It also unmounts the container's root filesystem.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) Cleanup(container *container.Container) {
|
2015-11-03 20:43:36 +03:00
|
|
|
daemon.releaseNetwork(container)
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
container.UnmountIpcMounts(detachMounted)
|
2015-11-03 20:43:36 +03:00
|
|
|
|
|
|
|
daemon.conditionalUnmountOnCleanup(container)
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
for _, eConfig := range container.ExecCommands.Commands() {
|
2015-11-21 01:35:16 +03:00
|
|
|
daemon.unregisterExecCommand(container, eConfig)
|
2015-11-03 20:43:36 +03:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
if err := container.UnmountVolumes(false); err != nil {
|
2015-11-03 20:43:36 +03:00
|
|
|
logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|