2014-07-31 16:46:18 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-07-16 18:33:13 -04:00
|
|
|
"runtime"
|
2015-03-25 03:44:12 -04:00
|
|
|
|
2015-11-03 12:43:36 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-11-03 12:33:13 -05:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-07-31 16:46:18 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerStart starts a container.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConfig) error {
|
|
|
|
container, err := daemon.Get(name)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
if container.isPaused() {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeStartPaused
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
if container.IsRunning() {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeAlreadyStarted
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 18:24:18 -04:00
|
|
|
// Windows does not have the backwards compatibility issue here.
|
2015-07-16 18:33:13 -04: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-10-28 09:19:51 -04:00
|
|
|
container.Lock()
|
|
|
|
if err := parseSecurityOpt(container, hostConfig); err != nil {
|
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Unlock()
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
2015-07-16 18:33:13 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hostConfig != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeHostConfigStart
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-08-06 07:55:56 -04:00
|
|
|
// check if hostConfig is in line with the current system settings.
|
|
|
|
// It may happen cgroups are umounted or the like.
|
2015-09-29 13:51:40 -04:00
|
|
|
if _, err = daemon.verifyContainerSettings(container.hostConfig, nil); err != nil {
|
2015-08-06 07:55:56 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
if err := daemon.containerStart(container); err != nil {
|
2015-07-29 08:21:16 -04:00
|
|
|
return err
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
2014-08-07 13:50:25 -04:00
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-31 16:46:18 -04:00
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
|
|
|
|
// Start starts a container
|
|
|
|
func (daemon *Daemon) Start(container *Container) error {
|
|
|
|
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.
|
|
|
|
func (daemon *Daemon) containerStart(container *Container) (err error) {
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
if container.Running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.removalInProgress || container.Dead {
|
|
|
|
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 {
|
|
|
|
container.setError(err)
|
|
|
|
// if no one else has set it, make sure we don't leave it at zero
|
|
|
|
if container.ExitCode == 0 {
|
|
|
|
container.ExitCode = 128
|
|
|
|
}
|
|
|
|
container.toDisk()
|
2015-11-03 12:43:36 -05:00
|
|
|
daemon.Cleanup(container)
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "die")
|
2015-11-02 20:06:09 -05: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.
|
|
|
|
container.hostConfig = runconfig.SetDefaultNetModeIfBlank(container.hostConfig)
|
|
|
|
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.initializeNetworking(container); err != nil {
|
2015-11-02 20:06:09 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-11-03 13:25:09 -05:00
|
|
|
linkedEnv, err := daemon.setupLinkedContainers(container)
|
2015-11-02 20:06:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := container.setupWorkingDirectory(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
env := container.createDaemonEnvironment(linkedEnv)
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.populateCommand(container, env); err != nil {
|
2015-11-02 20:06:09 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.hostConfig.IpcMode.IsContainer() && !container.hostConfig.IpcMode.IsHost() {
|
2015-11-03 13:25:09 -05:00
|
|
|
if err := daemon.setupIpcDirs(container); err != nil {
|
2015-11-02 20:06:09 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
mounts, err := daemon.setupMounts(container)
|
2015-11-02 20:06:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mounts = append(mounts, container.ipcMounts()...)
|
|
|
|
|
|
|
|
container.command.Mounts = mounts
|
2015-11-14 14:06:19 -05:00
|
|
|
if err := daemon.waitForStart(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.HasBeenStartedBefore = true
|
|
|
|
return nil
|
2015-11-03 12:33:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) waitForStart(container *Container) error {
|
|
|
|
container.monitor = daemon.newContainerMonitor(container, container.hostConfig.RestartPolicy)
|
|
|
|
|
|
|
|
// block until we either receive an error from the initial start of the container's
|
|
|
|
// process or until the process is running in the container
|
|
|
|
select {
|
|
|
|
case <-container.monitor.startSignal:
|
|
|
|
case err := <-promise.Go(container.monitor.Start):
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
2015-11-03 12:43:36 -05: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.
|
|
|
|
func (daemon *Daemon) Cleanup(container *Container) {
|
|
|
|
daemon.releaseNetwork(container)
|
|
|
|
|
|
|
|
container.unmountIpcMounts(detachMounted)
|
|
|
|
|
|
|
|
daemon.conditionalUnmountOnCleanup(container)
|
|
|
|
|
2015-11-20 17:35:16 -05:00
|
|
|
for _, eConfig := range container.execCommands.Commands() {
|
|
|
|
daemon.unregisterExecCommand(container, eConfig)
|
2015-11-03 12:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.unmountVolumes(false); err != nil {
|
|
|
|
logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|