4180b875 |
package daemon
import ( |
1d8ccc6a |
"fmt" |
a793564b |
"net/http" |
39ad38cc |
"runtime" |
9c4570a9 |
"strings"
"syscall" |
c79b9bab |
|
b4740e30 |
"google.golang.org/grpc"
|
019c337b |
"github.com/Sirupsen/logrus" |
6bb0d181 |
"github.com/docker/docker/container" |
a793564b |
"github.com/docker/docker/errors" |
9c4570a9 |
"github.com/docker/docker/libcontainerd" |
4180b875 |
"github.com/docker/docker/runconfig" |
907407d0 |
containertypes "github.com/docker/engine-api/types/container" |
4180b875 |
)
|
abd72d40 |
// ContainerStart starts a container. |
7ac4232e |
func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.HostConfig) error { |
d7d512bb |
container, err := daemon.GetContainer(name) |
d25a6537 |
if err != nil { |
c79b9bab |
return err |
4180b875 |
}
|
6bb0d181 |
if container.IsPaused() { |
a793564b |
return fmt.Errorf("Cannot start a paused container, try unpause instead.") |
02246d2d |
}
|
e0339d4b |
if container.IsRunning() { |
a793564b |
err := fmt.Errorf("Container already started")
return errors.NewErrorWithStatusCode(err, http.StatusNotModified) |
4180b875 |
}
|
51462327 |
// Windows does not have the backwards compatibility issue here. |
39ad38cc |
if runtime.GOOS != "windows" {
// This is kept for backward compatibility - hostconfig should be passed when
// creating a container, not during start.
if hostConfig != nil { |
d7117a1b |
logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12") |
3d2539d3 |
oldNetworkMode := container.HostConfig.NetworkMode |
d8e09066 |
if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
return err
} |
1790980e |
if err := daemon.mergeAndVerifyLogConfig(&hostConfig.LogConfig); err != nil {
return err
} |
b08f071e |
if err := daemon.setHostConfig(container, hostConfig); err != nil { |
39ad38cc |
return err
} |
3d2539d3 |
newNetworkMode := container.HostConfig.NetworkMode
if string(oldNetworkMode) != string(newNetworkMode) {
// if user has change the network mode on starting, clean up the
// old networks. It is a deprecated feature and will be removed in Docker 1.12
container.NetworkSettings.Networks = nil
if err := container.ToDisk(); err != nil {
return err
}
} |
6bb0d181 |
container.InitDNSHostConfig() |
39ad38cc |
}
} else {
if hostConfig != nil { |
a793564b |
return fmt.Errorf("Supplying a hostconfig on start is not supported. It should be supplied on create") |
4180b875 |
}
} |
767df67e |
|
4177b0ba |
// check if hostConfig is in line with the current system settings.
// It may happen cgroups are umounted or the like. |
8ae6f6ac |
if _, err = daemon.verifyContainerSettings(container.HostConfig, nil, false); err != nil { |
4177b0ba |
return err
} |
f408b4f4 |
// 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 |
4177b0ba |
}
|
c70f8b3c |
return daemon.containerStart(container) |
4180b875 |
} |
3a497650 |
// Start starts a container |
6bb0d181 |
func (daemon *Daemon) Start(container *container.Container) error { |
3a497650 |
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. |
6bb0d181 |
func (daemon *Daemon) containerStart(container *container.Container) (err error) { |
3a497650 |
container.Lock()
defer container.Unlock()
if container.Running {
return nil
}
|
6bb0d181 |
if container.RemovalInProgress || container.Dead { |
a793564b |
return fmt.Errorf("Container is marked for removal and cannot be started.") |
3a497650 |
}
// 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 { |
6bb0d181 |
container.SetError(err) |
3a497650 |
// if no one else has set it, make sure we don't leave it at zero |
dcfe9927 |
if container.ExitCode() == 0 {
container.SetExitCode(128) |
3a497650 |
} |
6bb0d181 |
container.ToDisk() |
019c337b |
daemon.Cleanup(container) |
3a497650 |
}
}()
if err := daemon.conditionalMountOnStart(container); err != nil {
return err
}
// Make sure NetworkMode has an acceptable value. We do this to ensure
// backwards API compatibility. |
6bb0d181 |
container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig) |
3a497650 |
|
669949d6 |
if err := daemon.initializeNetworking(container); err != nil { |
3a497650 |
return err
} |
9c4570a9 |
spec, err := daemon.createSpec(container) |
3a497650 |
if err != nil {
return err
}
|
7b2e5216 |
createOptions := []libcontainerd.CreateOption{libcontainerd.WithRestartManager(container.RestartManager(true))}
copts, err := daemon.getLibcontainerdCreateOptions(container)
if err != nil {
return err
}
if copts != nil {
createOptions = append(createOptions, *copts...)
}
if err := daemon.containerd.Create(container.ID, *spec, createOptions...); err != nil { |
b4740e30 |
errDesc := grpc.ErrorDesc(err)
logrus.Errorf("Create container failed with error: %s", errDesc) |
9c4570a9 |
// if we receive an internal error from the initial start of a container then lets
// return it instead of entering the restart loop
// set to 127 for container cmd not found/does not exist) |
7b2e5216 |
if strings.Contains(errDesc, container.Path) &&
(strings.Contains(errDesc, "executable file not found") ||
strings.Contains(errDesc, "no such file or directory") ||
strings.Contains(errDesc, "system cannot find the file specified")) { |
dcfe9927 |
container.SetExitCode(127) |
9c4570a9 |
}
// set to 126 for container cmd can't be invoked errors |
b4740e30 |
if strings.Contains(errDesc, syscall.EACCES.Error()) { |
dcfe9927 |
container.SetExitCode(126) |
3a497650 |
}
|
9c4570a9 |
container.Reset(false) |
fdfaaeb9 |
|
b4740e30 |
return fmt.Errorf("%s", errDesc) |
3a497650 |
}
|
8da44905 |
return nil |
ca5ede2d |
}
|
019c337b |
// 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. |
6bb0d181 |
func (daemon *Daemon) Cleanup(container *container.Container) { |
019c337b |
daemon.releaseNetwork(container)
|
6bb0d181 |
container.UnmountIpcMounts(detachMounted) |
019c337b |
|
9c4570a9 |
if err := daemon.conditionalUnmountOnCleanup(container); err != nil {
// FIXME: remove once reference counting for graphdrivers has been refactored
// Ensure that all the mounts are gone
if mountid, err := daemon.layerStore.GetMountID(container.ID); err == nil {
daemon.cleanupMountsByID(mountid)
}
} |
019c337b |
|
6bb0d181 |
for _, eConfig := range container.ExecCommands.Commands() { |
9ca2e4e8 |
daemon.unregisterExecCommand(container, eConfig) |
019c337b |
}
|
05cc737f |
if container.BaseFS != "" {
if err := container.UnmountVolumes(false, daemon.LogVolumeEvent); err != nil {
logrus.Warnf("%s cleanup: Failed to umount volumes: %v", container.ID, err)
} |
019c337b |
} |
7bb815e2 |
container.CancelAttachContext() |
019c337b |
} |