2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-05-24 11:49:26 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-05 11:56:42 -05:00
|
|
|
"os"
|
2017-08-04 14:39:23 -04:00
|
|
|
"path"
|
2016-05-24 11:49:26 -04:00
|
|
|
"path/filepath"
|
2017-08-04 14:39:23 -04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-05-24 11:49:26 -04:00
|
|
|
"time"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2016-05-24 11:49:26 -04:00
|
|
|
"github.com/docker/docker/container"
|
|
|
|
"github.com/docker/docker/daemon/network"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-05-24 11:49:26 -04:00
|
|
|
"github.com/docker/docker/image"
|
2016-12-23 14:09:12 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2016-05-27 05:32:26 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-05-24 11:49:26 -04:00
|
|
|
"github.com/docker/docker/pkg/truncindex"
|
2017-01-31 21:03:51 -05:00
|
|
|
"github.com/docker/docker/runconfig"
|
2017-12-19 05:44:29 -05:00
|
|
|
"github.com/docker/docker/volume"
|
2016-05-27 05:32:26 -04:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-04-05 20:37:04 -04:00
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-05-24 11:49:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetContainer looks for a container using the provided information, which could be
|
|
|
|
// one of the following inputs from the caller:
|
|
|
|
// - A full container ID, which will exact match a container in daemon's list
|
|
|
|
// - A container name, which will only exact match via the GetByName() function
|
|
|
|
// - A partial container ID prefix (e.g. short ID) of any length that is
|
|
|
|
// unique enough to only return a single container object
|
|
|
|
// If none of these searches succeed, an error is returned
|
|
|
|
func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, error) {
|
|
|
|
if len(prefixOrName) == 0 {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.WithStack(invalidIdentifier(prefixOrName))
|
2016-05-24 11:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if containerByID := daemon.containers.Get(prefixOrName); containerByID != nil {
|
|
|
|
// prefix is an exact match to a full container ID
|
|
|
|
return containerByID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByName will match only an exact name provided; we ignore errors
|
|
|
|
if containerByName, _ := daemon.GetByName(prefixOrName); containerByName != nil {
|
|
|
|
// prefix is an exact match to a full container Name
|
|
|
|
return containerByName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
containerID, indexError := daemon.idIndex.Get(prefixOrName)
|
|
|
|
if indexError != nil {
|
|
|
|
// When truncindex defines an error type, use that instead
|
|
|
|
if indexError == truncindex.ErrNotExist {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, containerNotFound(prefixOrName)
|
2016-05-24 11:49:26 -04:00
|
|
|
}
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(indexError)
|
2016-05-24 11:49:26 -04:00
|
|
|
}
|
|
|
|
return daemon.containers.Get(containerID), nil
|
|
|
|
}
|
|
|
|
|
2017-02-28 05:11:48 -05:00
|
|
|
// checkContainer make sure the specified container validates the specified conditions
|
|
|
|
func (daemon *Daemon) checkContainer(container *container.Container, conditions ...func(*container.Container) error) error {
|
|
|
|
for _, condition := range conditions {
|
|
|
|
if err := condition(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:49:26 -04:00
|
|
|
// Exists returns a true if a container of the specified ID or name exists,
|
|
|
|
// false otherwise.
|
|
|
|
func (daemon *Daemon) Exists(id string) bool {
|
|
|
|
c, _ := daemon.GetContainer(id)
|
|
|
|
return c != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPaused returns a bool indicating if the specified container is paused.
|
|
|
|
func (daemon *Daemon) IsPaused(id string) bool {
|
|
|
|
c, _ := daemon.GetContainer(id)
|
|
|
|
return c.State.IsPaused()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) containerRoot(id string) string {
|
|
|
|
return filepath.Join(daemon.repository, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load reads the contents of a container from disk
|
|
|
|
// This is typically done at startup.
|
|
|
|
func (daemon *Daemon) load(id string) (*container.Container, error) {
|
|
|
|
container := daemon.newBaseContainer(id)
|
|
|
|
|
|
|
|
if err := container.FromDisk(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-05 20:37:04 -04:00
|
|
|
if err := label.ReserveLabel(container.ProcessLabel); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-24 11:49:26 -04:00
|
|
|
|
|
|
|
if container.ID != id {
|
|
|
|
return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register makes a container object usable by the daemon as <container.ID>
|
2017-02-22 17:02:20 -05:00
|
|
|
func (daemon *Daemon) Register(c *container.Container) error {
|
2016-05-24 11:49:26 -04:00
|
|
|
// Attach to stdout and stderr
|
|
|
|
if c.Config.OpenStdin {
|
2016-11-14 15:15:09 -05:00
|
|
|
c.StreamConfig.NewInputPipes()
|
2016-05-24 11:49:26 -04:00
|
|
|
} else {
|
2016-11-14 15:15:09 -05:00
|
|
|
c.StreamConfig.NewNopInputPipe()
|
2016-05-24 11:49:26 -04:00
|
|
|
}
|
|
|
|
|
2017-02-22 17:02:20 -05:00
|
|
|
// once in the memory store it is visible to other goroutines
|
2017-02-23 18:12:18 -05:00
|
|
|
// grab a Lock until it has been checkpointed to avoid races
|
2017-02-22 17:02:20 -05:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2016-05-24 11:49:26 -04:00
|
|
|
daemon.containers.Add(c.ID, c)
|
|
|
|
daemon.idIndex.Add(c.ID)
|
2017-02-23 18:12:18 -05:00
|
|
|
return c.CheckpointTo(daemon.containersReplica)
|
2016-05-24 11:49:26 -04:00
|
|
|
}
|
|
|
|
|
2017-08-08 15:43:48 -04:00
|
|
|
func (daemon *Daemon) newContainer(name string, operatingSystem string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
|
2016-05-24 11:49:26 -04:00
|
|
|
var (
|
|
|
|
id string
|
|
|
|
err error
|
|
|
|
noExplicitName = name == ""
|
|
|
|
)
|
|
|
|
id, name, err = daemon.generateIDAndName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:56:42 -05:00
|
|
|
if hostConfig.NetworkMode.IsHost() {
|
|
|
|
if config.Hostname == "" {
|
|
|
|
config.Hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(err)
|
2016-12-05 11:56:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
daemon.generateHostname(id, config)
|
|
|
|
}
|
2016-05-24 11:49:26 -04:00
|
|
|
entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
|
|
|
|
|
|
|
|
base := daemon.newBaseContainer(id)
|
|
|
|
base.Created = time.Now().UTC()
|
2016-06-13 22:52:49 -04:00
|
|
|
base.Managed = managed
|
2016-05-24 11:49:26 -04:00
|
|
|
base.Path = entrypoint
|
|
|
|
base.Args = args //FIXME: de-duplicate from config
|
|
|
|
base.Config = config
|
|
|
|
base.HostConfig = &containertypes.HostConfig{}
|
|
|
|
base.ImageID = imgID
|
|
|
|
base.NetworkSettings = &network.Settings{IsAnonymousEndpoint: noExplicitName}
|
|
|
|
base.Name = name
|
2017-08-08 15:43:48 -04:00
|
|
|
base.Driver = daemon.GraphDriverName(operatingSystem)
|
|
|
|
base.OS = operatingSystem
|
2016-05-24 11:49:26 -04:00
|
|
|
return base, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByName returns a container given a name.
|
|
|
|
func (daemon *Daemon) GetByName(name string) (*container.Container, error) {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return nil, fmt.Errorf("No container name supplied")
|
|
|
|
}
|
|
|
|
fullName := name
|
|
|
|
if name[0] != '/' {
|
|
|
|
fullName = "/" + name
|
|
|
|
}
|
2017-06-29 21:56:22 -04:00
|
|
|
id, err := daemon.containersReplica.Snapshot().GetID(fullName)
|
2016-05-24 11:49:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not find entity for %s", name)
|
|
|
|
}
|
|
|
|
e := daemon.containers.Get(id)
|
|
|
|
if e == nil {
|
|
|
|
return nil, fmt.Errorf("Could not find container for entity id %s", id)
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
2016-05-27 05:32:26 -04:00
|
|
|
|
|
|
|
// newBaseContainer creates a new container with its initial
|
|
|
|
// configuration based on the root storage from the daemon.
|
|
|
|
func (daemon *Daemon) newBaseContainer(id string) *container.Container {
|
|
|
|
return container.NewBaseContainer(id, daemon.containerRoot(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) getEntrypointAndArgs(configEntrypoint strslice.StrSlice, configCmd strslice.StrSlice) (string, []string) {
|
|
|
|
if len(configEntrypoint) != 0 {
|
|
|
|
return configEntrypoint[0], append(configEntrypoint[1:], configCmd...)
|
|
|
|
}
|
|
|
|
return configCmd[0], configCmd[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) generateHostname(id string, config *containertypes.Config) {
|
|
|
|
// Generate default hostname
|
|
|
|
if config.Hostname == "" {
|
|
|
|
config.Hostname = id[:12]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) setSecurityOptions(container *container.Container, hostConfig *containertypes.HostConfig) error {
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2017-01-08 20:22:05 -05:00
|
|
|
return daemon.parseSecurityOpt(container, hostConfig)
|
2016-05-27 05:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *containertypes.HostConfig) error {
|
|
|
|
// Do not lock while creating volumes since this could be calling out to external plugins
|
|
|
|
// Don't want to block other actions, like `docker ps` because we're waiting on an external plugin
|
|
|
|
if err := daemon.registerMountPoints(container, hostConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
// Register any links from the host config before starting the container
|
|
|
|
if err := daemon.registerLinks(container, hostConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-31 21:03:51 -05:00
|
|
|
runconfig.SetDefaultNetModeIfBlank(hostConfig)
|
2016-05-27 05:32:26 -04:00
|
|
|
container.HostConfig = hostConfig
|
2017-03-27 13:18:53 -04:00
|
|
|
return container.CheckpointTo(daemon.containersReplica)
|
2016-05-27 05:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// verifyContainerSettings performs validation of the hostconfig and config
|
|
|
|
// structures.
|
2017-08-04 14:39:23 -04:00
|
|
|
func (daemon *Daemon) verifyContainerSettings(platform string, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) {
|
2016-05-27 05:32:26 -04:00
|
|
|
// First perform verification of settings common across all platforms.
|
|
|
|
if config != nil {
|
|
|
|
if config.WorkingDir != "" {
|
2017-08-04 14:39:23 -04:00
|
|
|
wdInvalid := false
|
|
|
|
if runtime.GOOS == platform {
|
|
|
|
config.WorkingDir = filepath.FromSlash(config.WorkingDir) // Ensure in platform semantics
|
|
|
|
if !system.IsAbs(config.WorkingDir) {
|
|
|
|
wdInvalid = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// LCOW. Force Unix semantics
|
|
|
|
config.WorkingDir = strings.Replace(config.WorkingDir, string(os.PathSeparator), "/", -1)
|
|
|
|
if !path.IsAbs(config.WorkingDir) {
|
|
|
|
wdInvalid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if wdInvalid {
|
2016-06-09 11:32:20 -04:00
|
|
|
return nil, fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", config.WorkingDir)
|
2016-05-27 05:32:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.StopSignal) > 0 {
|
|
|
|
_, err := signal.ParseSignal(config.StopSignal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 14:53:54 -04:00
|
|
|
// Validate if Env contains empty variable or not (e.g., ``, `=foo`)
|
|
|
|
for _, env := range config.Env {
|
|
|
|
if _, err := opts.ValidateEnv(env); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 02:55:45 -05:00
|
|
|
|
|
|
|
// Validate the healthcheck params of Config
|
|
|
|
if config.Healthcheck != nil {
|
2017-04-10 21:58:31 -04:00
|
|
|
if config.Healthcheck.Interval != 0 && config.Healthcheck.Interval < containertypes.MinimumDuration {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("Interval in Healthcheck cannot be less than %s", containertypes.MinimumDuration)
|
2017-01-17 02:55:45 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 21:58:31 -04:00
|
|
|
if config.Healthcheck.Timeout != 0 && config.Healthcheck.Timeout < containertypes.MinimumDuration {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("Timeout in Healthcheck cannot be less than %s", containertypes.MinimumDuration)
|
2017-01-17 02:55:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Healthcheck.Retries < 0 {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("Retries in Healthcheck cannot be negative")
|
2017-01-17 02:55:45 -05:00
|
|
|
}
|
2016-11-29 04:58:47 -05:00
|
|
|
|
2017-04-10 21:58:31 -04:00
|
|
|
if config.Healthcheck.StartPeriod != 0 && config.Healthcheck.StartPeriod < containertypes.MinimumDuration {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("StartPeriod in Healthcheck cannot be less than %s", containertypes.MinimumDuration)
|
2016-11-29 04:58:47 -05:00
|
|
|
}
|
2017-01-17 02:55:45 -05:00
|
|
|
}
|
2016-05-27 05:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if hostConfig == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-01 11:30:27 -05:00
|
|
|
if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("can't create 'AutoRemove' container with restart policy")
|
2016-03-01 11:30:27 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 05:44:29 -05:00
|
|
|
// Validate mounts; check if host directories still exist
|
|
|
|
parser := volume.NewParser(platform)
|
|
|
|
for _, cfg := range hostConfig.Mounts {
|
|
|
|
if err := parser.ValidateMountConfig(&cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 08:21:10 -05:00
|
|
|
for _, extraHost := range hostConfig.ExtraHosts {
|
|
|
|
if _, err := opts.ValidateExtraHost(extraHost); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 05:32:26 -04:00
|
|
|
for port := range hostConfig.PortBindings {
|
|
|
|
_, portStr := nat.SplitProtoPort(string(port))
|
|
|
|
if _, err := nat.ParsePort(portStr); err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("invalid port specification: %q", portStr)
|
2016-05-27 05:32:26 -04:00
|
|
|
}
|
|
|
|
for _, pb := range hostConfig.PortBindings[port] {
|
|
|
|
_, err := nat.NewPort(nat.SplitProtoPort(pb.HostPort))
|
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("invalid port specification: %q", pb.HostPort)
|
2016-05-27 05:32:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:33:55 -04:00
|
|
|
p := hostConfig.RestartPolicy
|
|
|
|
|
|
|
|
switch p.Name {
|
|
|
|
case "always", "unless-stopped", "no":
|
|
|
|
if p.MaximumRetryCount != 0 {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("maximum retry count cannot be used with restart policy '%s'", p.Name)
|
2016-06-28 18:33:55 -04:00
|
|
|
}
|
|
|
|
case "on-failure":
|
2016-12-01 16:24:30 -05:00
|
|
|
if p.MaximumRetryCount < 0 {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("maximum retry count cannot be negative")
|
2016-06-28 18:33:55 -04:00
|
|
|
}
|
|
|
|
case "":
|
2017-04-05 20:37:04 -04:00
|
|
|
// do nothing
|
2016-06-28 18:33:55 -04:00
|
|
|
default:
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("invalid restart policy '%s'", p.Name)
|
2016-06-28 18:33:55 -04:00
|
|
|
}
|
|
|
|
|
2017-11-14 08:36:25 -05:00
|
|
|
if !hostConfig.Isolation.IsValid() {
|
|
|
|
return nil, errors.Errorf("invalid isolation '%s' on %s", hostConfig.Isolation, runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
2016-05-27 05:32:26 -04:00
|
|
|
// Now do platform-specific verification
|
|
|
|
return verifyPlatformContainerSettings(daemon, hostConfig, config, update)
|
|
|
|
}
|