2014-02-12 04:04:39 +00:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2015-04-11 00:05:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2014-05-02 23:59:28 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-06-12 00:34:20 +00:00
|
|
|
"github.com/docker/docker/pkg/blkiodev"
|
2015-06-30 16:41:01 +00:00
|
|
|
"github.com/docker/docker/pkg/nat"
|
2015-09-01 18:26:48 +00:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-02-11 19:21:38 +00:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
2014-02-12 04:04:39 +00:00
|
|
|
)
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// KeyValuePair is a structure that hold a value for a key.
|
2015-03-29 21:17:23 +00:00
|
|
|
type KeyValuePair struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// NetworkMode represents the container network stack.
|
2014-05-02 23:59:28 +00:00
|
|
|
type NetworkMode string
|
|
|
|
|
2015-09-19 01:21:57 +00:00
|
|
|
// IsolationLevel represents the isolation level of a container. The supported
|
|
|
|
// values are platform specific
|
|
|
|
type IsolationLevel string
|
|
|
|
|
|
|
|
// IsDefault indicates the default isolation level of a container. On Linux this
|
2015-11-04 19:39:12 +00:00
|
|
|
// is the native driver. On Windows, this is a Windows Server Container.
|
2015-09-19 01:21:57 +00:00
|
|
|
func (i IsolationLevel) IsDefault() bool {
|
|
|
|
return strings.ToLower(string(i)) == "default" || string(i) == ""
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IpcMode represents the container ipc stack.
|
2014-11-10 21:14:17 +00:00
|
|
|
type IpcMode string
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsPrivate indicates whether the container uses it's private ipc stack.
|
2014-11-10 21:14:17 +00:00
|
|
|
func (n IpcMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost() || n.IsContainer())
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsHost indicates whether the container uses the host's ipc stack.
|
2014-11-10 21:14:17 +00:00
|
|
|
func (n IpcMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsContainer indicates whether the container uses a container's ipc stack.
|
2014-11-10 21:14:17 +00:00
|
|
|
func (n IpcMode) IsContainer() bool {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
return len(parts) > 1 && parts[0] == "container"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// Valid indicates whether the ipc stack is valid.
|
2014-11-10 21:14:17 +00:00
|
|
|
func (n IpcMode) Valid() bool {
|
|
|
|
parts := strings.Split(string(n), ":")
|
|
|
|
switch mode := parts[0]; mode {
|
|
|
|
case "", "host":
|
|
|
|
case "container":
|
|
|
|
if len(parts) != 2 || parts[1] == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// Container returns the name of the container ipc stack is going to be used.
|
2014-11-10 21:14:17 +00:00
|
|
|
func (n IpcMode) Container() string {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// UTSMode represents the UTS namespace of the container.
|
2015-05-05 22:32:36 +00:00
|
|
|
type UTSMode string
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsPrivate indicates whether the container uses it's private UTS namespace.
|
2015-05-05 22:32:36 +00:00
|
|
|
func (n UTSMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost())
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsHost indicates whether the container uses the host's UTS namespace.
|
2015-05-05 22:32:36 +00:00
|
|
|
func (n UTSMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// Valid indicates whether the UTS namespace is valid.
|
2015-05-05 22:32:36 +00:00
|
|
|
func (n UTSMode) Valid() bool {
|
|
|
|
parts := strings.Split(string(n), ":")
|
|
|
|
switch mode := parts[0]; mode {
|
|
|
|
case "", "host":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// PidMode represents the pid stack of the container.
|
2014-11-25 20:10:53 +00:00
|
|
|
type PidMode string
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsPrivate indicates whether the container uses it's private pid stack.
|
2014-11-25 20:10:53 +00:00
|
|
|
func (n PidMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost())
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsHost indicates whether the container uses the host's pid stack.
|
2014-11-25 20:10:53 +00:00
|
|
|
func (n PidMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// Valid indicates whether the pid stack is valid.
|
2014-11-25 20:10:53 +00:00
|
|
|
func (n PidMode) Valid() bool {
|
|
|
|
parts := strings.Split(string(n), ":")
|
|
|
|
switch mode := parts[0]; mode {
|
|
|
|
case "", "host":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// DeviceMapping represents the device mapping between the host and the container.
|
2014-05-31 04:00:47 +00:00
|
|
|
type DeviceMapping struct {
|
|
|
|
PathOnHost string
|
|
|
|
PathInContainer string
|
|
|
|
CgroupPermissions string
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// RestartPolicy represents the restart policies of the container.
|
2014-08-04 23:14:43 +00:00
|
|
|
type RestartPolicy struct {
|
|
|
|
Name string
|
|
|
|
MaximumRetryCount int
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsNone indicates whether the container has the "no" restart policy.
|
|
|
|
// This means the container will not automatically restart when exiting.
|
2015-05-16 11:34:09 +00:00
|
|
|
func (rp *RestartPolicy) IsNone() bool {
|
|
|
|
return rp.Name == "no"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsAlways indicates whether the container has the "always" restart policy.
|
|
|
|
// This means the container will automatically restart regardless of the exit status.
|
2015-05-16 11:34:09 +00:00
|
|
|
func (rp *RestartPolicy) IsAlways() bool {
|
|
|
|
return rp.Name == "always"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// IsOnFailure indicates whether the container has the "on-failure" restart policy.
|
|
|
|
// This means the contain will automatically restart of exiting with a non-zero exit status.
|
2015-05-16 11:34:09 +00:00
|
|
|
func (rp *RestartPolicy) IsOnFailure() bool {
|
|
|
|
return rp.Name == "on-failure"
|
|
|
|
}
|
|
|
|
|
2015-08-05 21:09:08 +00:00
|
|
|
// IsUnlessStopped indicates whether the container has the
|
|
|
|
// "unless-stopped" restart policy. This means the container will
|
|
|
|
// automatically restart unless user has put it to stopped state.
|
|
|
|
func (rp *RestartPolicy) IsUnlessStopped() bool {
|
|
|
|
return rp.Name == "unless-stopped"
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// LogConfig represents the logging configuration of the container.
|
2015-02-04 19:04:58 +00:00
|
|
|
type LogConfig struct {
|
|
|
|
Type string
|
|
|
|
Config map[string]string
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:03:08 +00:00
|
|
|
// Resources contains container's resources (cgroups config, ulimits...)
|
|
|
|
type Resources struct {
|
|
|
|
// Applicable to all platforms
|
|
|
|
CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
|
|
|
|
|
|
|
|
// Applicable to UNIX platforms
|
|
|
|
CgroupParent string // Parent cgroup.
|
|
|
|
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
|
|
|
|
BlkioWeightDevice []*blkiodev.WeightDevice
|
|
|
|
CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
|
|
|
|
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
|
|
|
|
CpusetCpus string // CpusetCpus 0-2, 0,1
|
|
|
|
CpusetMems string // CpusetMems 0-2, 0,1
|
|
|
|
Devices []DeviceMapping // List of devices to map inside the container
|
|
|
|
KernelMemory int64 // Kernel memory limit (in bytes)
|
|
|
|
Memory int64 // Memory limit (in bytes)
|
|
|
|
MemoryReservation int64 // Memory soft limit (in bytes)
|
|
|
|
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to disable swap
|
|
|
|
MemorySwappiness *int64 // Tuning container memory swappiness behaviour
|
|
|
|
Ulimits []*ulimit.Ulimit // List of ulimits to be set in the container
|
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
// HostConfig the non-portable Config structure of a container.
|
|
|
|
// Here, "non-portable" means "dependent of the host we are running on".
|
|
|
|
// Portable information *should* appear in Config.
|
2014-02-12 04:04:39 +00:00
|
|
|
type HostConfig struct {
|
2015-11-09 23:24:23 +00:00
|
|
|
// Applicable to all platforms
|
|
|
|
Binds []string // List of volume bindings for this container
|
|
|
|
ContainerIDFile string // File (path) where the containerId is written
|
|
|
|
LogConfig LogConfig // Configuration of the logs for this container
|
|
|
|
NetworkMode NetworkMode // Network mode to use for the container
|
|
|
|
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
|
|
|
|
RestartPolicy RestartPolicy // Restart policy to be used for the container
|
|
|
|
VolumeDriver string // Name of the volume driver used to mount volumes
|
|
|
|
VolumesFrom []string // List of volumes to take from other container
|
|
|
|
|
|
|
|
// Applicable to UNIX platforms
|
2015-11-18 19:03:08 +00:00
|
|
|
CapAdd *stringutils.StrSlice // List of kernel capabilities to add to the container
|
|
|
|
CapDrop *stringutils.StrSlice // List of kernel capabilities to remove from the container
|
|
|
|
DNS []string `json:"Dns"` // List of DNS server to lookup
|
|
|
|
DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for
|
|
|
|
DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for
|
|
|
|
ExtraHosts []string // List of extra hosts
|
|
|
|
GroupAdd []string // List of additional groups that the container process will run as
|
|
|
|
IpcMode IpcMode // IPC namespace to use for the container
|
|
|
|
Links []string // List of links (in the name:alias form)
|
2015-10-13 09:26:27 +00:00
|
|
|
OomScoreAdj int // Container preference for OOM-killing
|
2015-11-18 19:03:08 +00:00
|
|
|
OomKillDisable bool // Whether to disable OOM Killer or not
|
|
|
|
PidMode PidMode // PID namespace to use for the container
|
|
|
|
Privileged bool // Is the container in privileged mode
|
|
|
|
PublishAllPorts bool // Should docker publish all exposed port for the container
|
|
|
|
ReadonlyRootfs bool // Is the container root filesystem in read-only
|
|
|
|
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux.
|
2015-12-01 18:39:34 +00:00
|
|
|
Tmpfs map[string]string `json:",omitempty"` // List of tmpfs (mounts) used for the container
|
2015-11-18 19:03:08 +00:00
|
|
|
UTSMode UTSMode // UTS namespace to use for the container
|
2015-11-26 12:14:09 +00:00
|
|
|
ShmSize *int64 // Total shm memory usage
|
2015-11-09 23:24:23 +00:00
|
|
|
|
|
|
|
// Applicable to Windows
|
|
|
|
ConsoleSize [2]int // Initial console size
|
|
|
|
Isolation IsolationLevel // Isolation level of the container (eg default, hyperv)
|
2015-11-18 19:03:08 +00:00
|
|
|
|
|
|
|
// Contains container's resources (cgroups, ulimits)
|
|
|
|
Resources
|
2015-07-25 09:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeHostConfig creates a HostConfig based on the specified Reader.
|
|
|
|
// It assumes the content of the reader will be JSON, and decodes it.
|
2015-04-11 00:05:21 +00:00
|
|
|
func DecodeHostConfig(src io.Reader) (*HostConfig, error) {
|
|
|
|
decoder := json.NewDecoder(src)
|
|
|
|
|
2015-05-14 14:39:44 +00:00
|
|
|
var w ContainerConfigWrapper
|
2015-04-11 00:05:21 +00:00
|
|
|
if err := decoder.Decode(&w); err != nil {
|
|
|
|
return nil, err
|
2014-07-10 21:51:15 +00:00
|
|
|
}
|
2014-08-04 23:14:43 +00:00
|
|
|
|
2015-07-09 22:12:36 +00:00
|
|
|
hc := w.getHostConfig()
|
2015-04-11 00:05:21 +00:00
|
|
|
return hc, nil
|
2014-02-12 04:04:39 +00:00
|
|
|
}
|
2015-07-09 22:12:36 +00:00
|
|
|
|
|
|
|
// SetDefaultNetModeIfBlank changes the NetworkMode in a HostConfig structure
|
|
|
|
// to default if it is not populated. This ensures backwards compatibility after
|
|
|
|
// the validation of the network mode was moved from the docker CLI to the
|
|
|
|
// docker daemon.
|
|
|
|
func SetDefaultNetModeIfBlank(hc *HostConfig) *HostConfig {
|
|
|
|
if hc != nil {
|
|
|
|
if hc.NetworkMode == NetworkMode("") {
|
|
|
|
hc.NetworkMode = NetworkMode("default")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hc
|
|
|
|
}
|