2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
import (
|
2016-01-04 10:58:20 -05:00
|
|
|
"fmt"
|
2016-01-27 16:03:09 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-02-22 12:11:10 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-01-17 10:49:58 -05:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2016-12-01 11:11:15 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2015-11-12 14:55:17 -05:00
|
|
|
)
|
|
|
|
|
2017-04-11 13:34:19 -04:00
|
|
|
const (
|
2016-12-01 11:11:15 -05:00
|
|
|
containerSecretMountPath = `C:\ProgramData\Docker\secrets`
|
|
|
|
containerInternalSecretMountPath = `C:\ProgramData\Docker\internal\secrets`
|
2017-05-11 14:55:03 -04:00
|
|
|
containerInternalConfigsDirPath = `C:\ProgramData\Docker\internal\configs`
|
2017-10-12 19:31:33 -04:00
|
|
|
|
|
|
|
// DefaultStopTimeout is the timeout (in seconds) for the shutdown call on a container
|
|
|
|
DefaultStopTimeout = 30
|
2017-04-11 13:34:19 -04:00
|
|
|
)
|
|
|
|
|
Implement none, private, and shareable ipc modes
Since the commit d88fe447df0e8 ("Add support for sharing /dev/shm/ and
/dev/mqueue between containers") container's /dev/shm is mounted on the
host first, then bind-mounted inside the container. This is done that
way in order to be able to share this container's IPC namespace
(and the /dev/shm mount point) with another container.
Unfortunately, this functionality breaks container checkpoint/restore
(even if IPC is not shared). Since /dev/shm is an external mount, its
contents is not saved by `criu checkpoint`, and so upon restore any
application that tries to access data under /dev/shm is severily
disappointed (which usually results in a fatal crash).
This commit solves the issue by introducing new IPC modes for containers
(in addition to 'host' and 'container:ID'). The new modes are:
- 'shareable': enables sharing this container's IPC with others
(this used to be the implicit default);
- 'private': disables sharing this container's IPC.
In 'private' mode, container's /dev/shm is truly mounted inside the
container, without any bind-mounting from the host, which solves the
issue.
While at it, let's also implement 'none' mode. The motivation, as
eloquently put by Justin Cormack, is:
> I wondered a while back about having a none shm mode, as currently it is
> not possible to have a totally unwriteable container as there is always
> a /dev/shm writeable mount. It is a bit of a niche case (and clearly
> should never be allowed to be daemon default) but it would be trivial to
> add now so maybe we should...
...so here's yet yet another mode:
- 'none': no /dev/shm mount inside the container (though it still
has its own private IPC namespace).
Now, to ultimately solve the abovementioned checkpoint/restore issue, we'd
need to make 'private' the default mode, but unfortunately it breaks the
backward compatibility. So, let's make the default container IPC mode
per-daemon configurable (with the built-in default set to 'shareable'
for now). The default can be changed either via a daemon CLI option
(--default-shm-mode) or a daemon.json configuration file parameter
of the same name.
Note one can only set either 'shareable' or 'private' IPC modes as a
daemon default (i.e. in this context 'host', 'container', or 'none'
do not make much sense).
Some other changes this patch introduces are:
1. A mount for /dev/shm is added to default OCI Linux spec.
2. IpcMode.Valid() is simplified to remove duplicated code that parsed
'container:ID' form. Note the old version used to check that ID does
not contain a semicolon -- this is no longer the case (tests are
modified accordingly). The motivation is we should either do a
proper check for container ID validity, or don't check it at all
(since it is checked in other places anyway). I chose the latter.
3. IpcMode.Container() is modified to not return container ID if the
mode value does not start with "container:", unifying the check to
be the same as in IpcMode.IsContainer().
3. IPC mode unit tests (runconfig/hostconfig_test.go) are modified
to add checks for newly added values.
[v2: addressed review at https://github.com/moby/moby/pull/34087#pullrequestreview-51345997]
[v3: addressed review at https://github.com/moby/moby/pull/34087#pullrequestreview-53902833]
[v4: addressed the case of upgrading from older daemon, in this case
container.HostConfig.IpcMode is unset and this is valid]
[v5: document old and new IpcMode values in api/swagger.yaml]
[v6: add the 'none' mode, changelog entry to docs/api/version-history.md]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-06-27 17:58:50 -04:00
|
|
|
// UnmountIpcMount unmounts Ipc related mounts.
|
2015-11-12 14:55:17 -05:00
|
|
|
// This is a NOOP on windows.
|
2018-10-24 20:29:03 -04:00
|
|
|
func (container *Container) UnmountIpcMount() error {
|
Implement none, private, and shareable ipc modes
Since the commit d88fe447df0e8 ("Add support for sharing /dev/shm/ and
/dev/mqueue between containers") container's /dev/shm is mounted on the
host first, then bind-mounted inside the container. This is done that
way in order to be able to share this container's IPC namespace
(and the /dev/shm mount point) with another container.
Unfortunately, this functionality breaks container checkpoint/restore
(even if IPC is not shared). Since /dev/shm is an external mount, its
contents is not saved by `criu checkpoint`, and so upon restore any
application that tries to access data under /dev/shm is severily
disappointed (which usually results in a fatal crash).
This commit solves the issue by introducing new IPC modes for containers
(in addition to 'host' and 'container:ID'). The new modes are:
- 'shareable': enables sharing this container's IPC with others
(this used to be the implicit default);
- 'private': disables sharing this container's IPC.
In 'private' mode, container's /dev/shm is truly mounted inside the
container, without any bind-mounting from the host, which solves the
issue.
While at it, let's also implement 'none' mode. The motivation, as
eloquently put by Justin Cormack, is:
> I wondered a while back about having a none shm mode, as currently it is
> not possible to have a totally unwriteable container as there is always
> a /dev/shm writeable mount. It is a bit of a niche case (and clearly
> should never be allowed to be daemon default) but it would be trivial to
> add now so maybe we should...
...so here's yet yet another mode:
- 'none': no /dev/shm mount inside the container (though it still
has its own private IPC namespace).
Now, to ultimately solve the abovementioned checkpoint/restore issue, we'd
need to make 'private' the default mode, but unfortunately it breaks the
backward compatibility. So, let's make the default container IPC mode
per-daemon configurable (with the built-in default set to 'shareable'
for now). The default can be changed either via a daemon CLI option
(--default-shm-mode) or a daemon.json configuration file parameter
of the same name.
Note one can only set either 'shareable' or 'private' IPC modes as a
daemon default (i.e. in this context 'host', 'container', or 'none'
do not make much sense).
Some other changes this patch introduces are:
1. A mount for /dev/shm is added to default OCI Linux spec.
2. IpcMode.Valid() is simplified to remove duplicated code that parsed
'container:ID' form. Note the old version used to check that ID does
not contain a semicolon -- this is no longer the case (tests are
modified accordingly). The motivation is we should either do a
proper check for container ID validity, or don't check it at all
(since it is checked in other places anyway). I chose the latter.
3. IpcMode.Container() is modified to not return container ID if the
mode value does not start with "container:", unifying the check to
be the same as in IpcMode.IsContainer().
3. IPC mode unit tests (runconfig/hostconfig_test.go) are modified
to add checks for newly added values.
[v2: addressed review at https://github.com/moby/moby/pull/34087#pullrequestreview-51345997]
[v3: addressed review at https://github.com/moby/moby/pull/34087#pullrequestreview-53902833]
[v4: addressed the case of upgrading from older daemon, in this case
container.HostConfig.IpcMode is unset and this is valid]
[v5: document old and new IpcMode values in api/swagger.yaml]
[v6: add the 'none' mode, changelog entry to docs/api/version-history.md]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-06-27 17:58:50 -04:00
|
|
|
return nil
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IpcMounts returns the list of Ipc related mounts.
|
2016-03-18 14:53:27 -04:00
|
|
|
func (container *Container) IpcMounts() []Mount {
|
2015-11-12 14:55:17 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:11:15 -05:00
|
|
|
// CreateSecretSymlinks creates symlinks to files in the secret mount.
|
|
|
|
func (container *Container) CreateSecretSymlinks() error {
|
|
|
|
for _, r := range container.SecretReferences {
|
|
|
|
if r.File == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resolvedPath, _, err := container.ResolvePath(getSecretTargetPath(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-01 21:59:11 -04:00
|
|
|
if err := system.MkdirAll(filepath.Dir(resolvedPath), 0, ""); err != nil {
|
2016-12-01 11:11:15 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink(filepath.Join(containerInternalSecretMountPath, r.SecretID), resolvedPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 16:51:13 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 11:11:15 -05:00
|
|
|
// SecretMounts returns the mount for the secret path.
|
|
|
|
// All secrets are stored in a single mount on Windows. Target symlinks are
|
|
|
|
// created for each secret, pointing to the files in this mount.
|
2017-12-18 16:02:23 -05:00
|
|
|
func (container *Container) SecretMounts() ([]Mount, error) {
|
2016-12-01 11:11:15 -05:00
|
|
|
var mounts []Mount
|
|
|
|
if len(container.SecretReferences) > 0 {
|
2017-12-18 16:02:23 -05:00
|
|
|
src, err := container.SecretMountPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-01 11:11:15 -05:00
|
|
|
mounts = append(mounts, Mount{
|
2017-12-18 16:02:23 -05:00
|
|
|
Source: src,
|
2016-12-01 11:11:15 -05:00
|
|
|
Destination: containerInternalSecretMountPath,
|
|
|
|
Writable: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-18 16:02:23 -05:00
|
|
|
return mounts, nil
|
2016-12-01 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2016-10-27 16:51:13 -04:00
|
|
|
// UnmountSecrets unmounts the fs for secrets
|
|
|
|
func (container *Container) UnmountSecrets() error {
|
2017-12-18 16:02:23 -05:00
|
|
|
p, err := container.SecretMountPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(p)
|
2016-10-27 16:51:13 -04:00
|
|
|
}
|
|
|
|
|
2017-05-11 14:55:03 -04:00
|
|
|
// CreateConfigSymlinks creates symlinks to files in the config mount.
|
|
|
|
func (container *Container) CreateConfigSymlinks() error {
|
|
|
|
for _, configRef := range container.ConfigReferences {
|
|
|
|
if configRef.File == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resolvedPath, _, err := container.ResolvePath(configRef.File.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-01 21:59:11 -04:00
|
|
|
if err := system.MkdirAll(filepath.Dir(resolvedPath), 0, ""); err != nil {
|
2017-05-11 14:55:03 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink(filepath.Join(containerInternalConfigsDirPath, configRef.ConfigID), resolvedPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigMounts returns the mount for configs.
|
2018-01-17 10:49:58 -05:00
|
|
|
// TODO: Right now Windows doesn't really have a "secure" storage for secrets,
|
|
|
|
// however some configs may contain secrets. Once secure storage is worked out,
|
|
|
|
// configs and secret handling should be merged.
|
|
|
|
func (container *Container) ConfigMounts() []Mount {
|
2017-05-11 14:55:03 -04:00
|
|
|
var mounts []Mount
|
|
|
|
if len(container.ConfigReferences) > 0 {
|
|
|
|
mounts = append(mounts, Mount{
|
2018-01-17 10:49:58 -05:00
|
|
|
Source: container.ConfigsDirPath(),
|
2017-05-11 14:55:03 -04:00
|
|
|
Destination: containerInternalConfigsDirPath,
|
|
|
|
Writable: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:49:58 -05:00
|
|
|
return mounts
|
2017-05-11 14:55:03 -04:00
|
|
|
}
|
|
|
|
|
2016-10-03 13:53:06 -04:00
|
|
|
// DetachAndUnmount unmounts all volumes.
|
|
|
|
// On Windows it only delegates to `UnmountVolumes` since there is nothing to
|
|
|
|
// force unmount.
|
|
|
|
func (container *Container) DetachAndUnmount(volumeEventLog func(name, action string, attributes map[string]string)) error {
|
|
|
|
return container.UnmountVolumes(volumeEventLog)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TmpfsMounts returns the list of tmpfs mounts
|
2016-09-22 16:14:15 -04:00
|
|
|
func (container *Container) TmpfsMounts() ([]Mount, error) {
|
2016-06-06 05:57:11 -04:00
|
|
|
var mounts []Mount
|
2016-09-22 16:14:15 -04:00
|
|
|
return mounts, nil
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
2017-02-22 17:02:20 -05:00
|
|
|
// UpdateContainer updates configuration of a container. Callers must hold a Lock on the Container.
|
2016-03-01 13:23:43 -05:00
|
|
|
func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfig) error {
|
2016-01-04 10:58:20 -05:00
|
|
|
resources := hostConfig.Resources
|
2017-03-06 21:29:27 -05:00
|
|
|
if resources.CPUShares != 0 ||
|
|
|
|
resources.Memory != 0 ||
|
|
|
|
resources.NanoCPUs != 0 ||
|
|
|
|
resources.CgroupParent != "" ||
|
|
|
|
resources.BlkioWeight != 0 ||
|
|
|
|
len(resources.BlkioWeightDevice) != 0 ||
|
|
|
|
len(resources.BlkioDeviceReadBps) != 0 ||
|
|
|
|
len(resources.BlkioDeviceWriteBps) != 0 ||
|
|
|
|
len(resources.BlkioDeviceReadIOps) != 0 ||
|
|
|
|
len(resources.BlkioDeviceWriteIOps) != 0 ||
|
|
|
|
resources.CPUPeriod != 0 ||
|
|
|
|
resources.CPUQuota != 0 ||
|
|
|
|
resources.CPURealtimePeriod != 0 ||
|
|
|
|
resources.CPURealtimeRuntime != 0 ||
|
|
|
|
resources.CpusetCpus != "" ||
|
|
|
|
resources.CpusetMems != "" ||
|
|
|
|
len(resources.Devices) != 0 ||
|
|
|
|
len(resources.DeviceCgroupRules) != 0 ||
|
|
|
|
resources.KernelMemory != 0 ||
|
|
|
|
resources.MemoryReservation != 0 ||
|
|
|
|
resources.MemorySwap != 0 ||
|
|
|
|
resources.MemorySwappiness != nil ||
|
|
|
|
resources.OomKillDisable != nil ||
|
2017-04-11 07:28:13 -04:00
|
|
|
(resources.PidsLimit != nil && *resources.PidsLimit != 0) ||
|
2017-03-06 21:29:27 -05:00
|
|
|
len(resources.Ulimits) != 0 ||
|
|
|
|
resources.CPUCount != 0 ||
|
|
|
|
resources.CPUPercent != 0 ||
|
|
|
|
resources.IOMaximumIOps != 0 ||
|
|
|
|
resources.IOMaximumBandwidth != 0 {
|
|
|
|
return fmt.Errorf("resource updating isn't supported on Windows")
|
2016-01-04 10:58:20 -05:00
|
|
|
}
|
|
|
|
// update HostConfig of container
|
|
|
|
if hostConfig.RestartPolicy.Name != "" {
|
2016-08-15 04:38:47 -04:00
|
|
|
if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
|
|
|
|
return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container")
|
|
|
|
}
|
2016-01-04 10:58:20 -05:00
|
|
|
container.HostConfig.RestartPolicy = hostConfig.RestartPolicy
|
|
|
|
}
|
2015-12-28 06:19:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
// BuildHostnameFile writes the container's hostname file.
|
|
|
|
func (container *Container) BuildHostnameFile() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-22 12:11:10 -05:00
|
|
|
// GetMountPoints gives a platform specific transformation to types.MountPoint. Callers must hold a Container lock.
|
|
|
|
func (container *Container) GetMountPoints() []types.MountPoint {
|
|
|
|
mountPoints := make([]types.MountPoint, 0, len(container.MountPoints))
|
|
|
|
for _, m := range container.MountPoints {
|
|
|
|
mountPoints = append(mountPoints, types.MountPoint{
|
|
|
|
Type: m.Type,
|
|
|
|
Name: m.Name,
|
|
|
|
Source: m.Path(),
|
|
|
|
Destination: m.Destination,
|
|
|
|
Driver: m.Driver,
|
|
|
|
RW: m.RW,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return mountPoints
|
|
|
|
}
|
2018-01-17 10:49:58 -05:00
|
|
|
|
|
|
|
func (container *Container) ConfigsDirPath() string {
|
|
|
|
return filepath.Join(container.Root, "configs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFilePath returns the path to the on-disk location of a config.
|
|
|
|
func (container *Container) ConfigFilePath(configRef swarmtypes.ConfigReference) string {
|
|
|
|
return filepath.Join(container.ConfigsDirPath(), configRef.ConfigID)
|
|
|
|
}
|