2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2014-05-02 19:59:28 -04:00
|
|
|
"strings"
|
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/nat"
|
2015-02-11 14:21:38 -05:00
|
|
|
"github.com/docker/docker/pkg/ulimit"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2014-05-02 19:59:28 -04:00
|
|
|
type NetworkMode string
|
|
|
|
|
2014-09-05 02:43:44 -04:00
|
|
|
// IsPrivate indicates whether container use it's private network stack
|
|
|
|
func (n NetworkMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost() || n.IsContainer() || n.IsNone())
|
|
|
|
}
|
|
|
|
|
2014-05-02 19:59:28 -04:00
|
|
|
func (n NetworkMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NetworkMode) IsContainer() bool {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
return len(parts) > 1 && parts[0] == "container"
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:50:58 -04:00
|
|
|
func (n NetworkMode) IsNone() bool {
|
|
|
|
return n == "none"
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:14:17 -05:00
|
|
|
type IpcMode string
|
|
|
|
|
|
|
|
// IsPrivate indicates whether container use it's private ipc stack
|
|
|
|
func (n IpcMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost() || n.IsContainer())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n IpcMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n IpcMode) IsContainer() bool {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
return len(parts) > 1 && parts[0] == "container"
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n IpcMode) Container() string {
|
|
|
|
parts := strings.SplitN(string(n), ":", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
type PidMode string
|
|
|
|
|
|
|
|
// IsPrivate indicates whether container use it's private pid stack
|
|
|
|
func (n PidMode) IsPrivate() bool {
|
|
|
|
return !(n.IsHost())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n PidMode) IsHost() bool {
|
|
|
|
return n == "host"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n PidMode) Valid() bool {
|
|
|
|
parts := strings.Split(string(n), ":")
|
|
|
|
switch mode := parts[0]; mode {
|
|
|
|
case "", "host":
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-31 00:00:47 -04:00
|
|
|
type DeviceMapping struct {
|
|
|
|
PathOnHost string
|
|
|
|
PathInContainer string
|
|
|
|
CgroupPermissions string
|
|
|
|
}
|
|
|
|
|
2014-08-04 19:14:43 -04:00
|
|
|
type RestartPolicy struct {
|
|
|
|
Name string
|
|
|
|
MaximumRetryCount int
|
|
|
|
}
|
|
|
|
|
2015-02-04 14:04:58 -05:00
|
|
|
type LogConfig struct {
|
|
|
|
Type string
|
|
|
|
Config map[string]string
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
type HostConfig struct {
|
2014-05-02 17:06:05 -04:00
|
|
|
Binds []string
|
|
|
|
ContainerIDFile string
|
|
|
|
LxcConf []utils.KeyValuePair
|
move resources from Config to HostConfig
Cgroup resources are host dependent, they should be in hostConfig.
For backward compatibility, we just copy it to hostConfig, and leave it in
Config for now, so there is no regressions, but the right way to use this
throught json is to put it in HostConfig, like:
{
"Hostname": "",
...
"HostConfig": {
"CpuShares": 512,
"Memory": 314572800,
...
}
}
As we will add CpusetMems, CpusetCpus is definitely a better name, but some
users are already using Cpuset in their http APIs, we also make it compatible.
The main idea is keep using Cpuset in Config Struct, and make it has the same
value as CpusetCpus, but not always, some scenarios:
- Users use --cpuset in docker command, it can setup cpuset.cpus and can
get Cpuset field from docker inspect or other http API which will get
config info.
- Users use --cpuset-cpus in docker command, ditto.
- Users use Cpuset field in their http APIs, ditto.
- Users use CpusetCpus field in their http APIs, they won't get Cpuset field
in Config info, because by then, they should already know what happens
to Cpuset.
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
2015-03-10 21:31:18 -04:00
|
|
|
Memory int64 // Memory limit (in bytes)
|
|
|
|
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to disable swap
|
|
|
|
CpuShares int64 // CPU shares (relative weight vs. other containers)
|
|
|
|
CpusetCpus string // CpusetCpus 0-2, 0,1
|
2014-05-02 17:06:05 -04:00
|
|
|
Privileged bool
|
|
|
|
PortBindings nat.PortMap
|
|
|
|
Links []string
|
|
|
|
PublishAllPorts bool
|
|
|
|
Dns []string
|
|
|
|
DnsSearch []string
|
2014-09-13 00:35:59 -04:00
|
|
|
ExtraHosts []string
|
2014-05-02 17:06:05 -04:00
|
|
|
VolumesFrom []string
|
2014-05-31 00:00:47 -04:00
|
|
|
Devices []DeviceMapping
|
2014-05-02 19:59:28 -04:00
|
|
|
NetworkMode NetworkMode
|
2014-11-10 16:14:17 -05:00
|
|
|
IpcMode IpcMode
|
2014-11-25 15:10:53 -05:00
|
|
|
PidMode PidMode
|
2014-07-10 14:41:11 -04:00
|
|
|
CapAdd []string
|
|
|
|
CapDrop []string
|
2014-08-04 19:14:43 -04:00
|
|
|
RestartPolicy RestartPolicy
|
2014-11-03 17:57:18 -05:00
|
|
|
SecurityOpt []string
|
2015-01-13 16:52:51 -05:00
|
|
|
ReadonlyRootfs bool
|
2015-02-11 14:21:38 -05:00
|
|
|
Ulimits []*ulimit.Ulimit
|
2015-02-04 14:04:58 -05:00
|
|
|
LogConfig LogConfig
|
2015-03-16 18:42:15 -04:00
|
|
|
CgroupParent string // Parent cgroup.
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
|
|
|
|
2014-07-18 20:38:27 -04:00
|
|
|
// This is used by the create command when you want to set both the
|
2014-03-10 09:11:23 -04:00
|
|
|
// Config and the HostConfig in the same call
|
|
|
|
type ConfigAndHostConfig struct {
|
|
|
|
Config
|
|
|
|
HostConfig HostConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func MergeConfigs(config *Config, hostConfig *HostConfig) *ConfigAndHostConfig {
|
|
|
|
return &ConfigAndHostConfig{
|
|
|
|
*config,
|
|
|
|
*hostConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
|
2014-03-10 09:11:23 -04:00
|
|
|
if job.EnvExists("HostConfig") {
|
|
|
|
hostConfig := HostConfig{}
|
|
|
|
job.GetenvJson("HostConfig", &hostConfig)
|
move resources from Config to HostConfig
Cgroup resources are host dependent, they should be in hostConfig.
For backward compatibility, we just copy it to hostConfig, and leave it in
Config for now, so there is no regressions, but the right way to use this
throught json is to put it in HostConfig, like:
{
"Hostname": "",
...
"HostConfig": {
"CpuShares": 512,
"Memory": 314572800,
...
}
}
As we will add CpusetMems, CpusetCpus is definitely a better name, but some
users are already using Cpuset in their http APIs, we also make it compatible.
The main idea is keep using Cpuset in Config Struct, and make it has the same
value as CpusetCpus, but not always, some scenarios:
- Users use --cpuset in docker command, it can setup cpuset.cpus and can
get Cpuset field from docker inspect or other http API which will get
config info.
- Users use --cpuset-cpus in docker command, ditto.
- Users use Cpuset field in their http APIs, ditto.
- Users use CpusetCpus field in their http APIs, they won't get Cpuset field
in Config info, because by then, they should already know what happens
to Cpuset.
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
2015-03-10 21:31:18 -04:00
|
|
|
|
|
|
|
// FIXME: These are for backward compatibility, if people use these
|
|
|
|
// options with `HostConfig`, we should still make them workable.
|
|
|
|
if job.EnvExists("Memory") && hostConfig.Memory == 0 {
|
|
|
|
hostConfig.Memory = job.GetenvInt64("Memory")
|
|
|
|
}
|
|
|
|
if job.EnvExists("MemorySwap") && hostConfig.MemorySwap == 0 {
|
|
|
|
hostConfig.MemorySwap = job.GetenvInt64("MemorySwap")
|
|
|
|
}
|
|
|
|
if job.EnvExists("CpuShares") && hostConfig.CpuShares == 0 {
|
|
|
|
hostConfig.CpuShares = job.GetenvInt64("CpuShares")
|
|
|
|
}
|
|
|
|
if job.EnvExists("Cpuset") && hostConfig.CpusetCpus == "" {
|
|
|
|
hostConfig.CpusetCpus = job.Getenv("Cpuset")
|
|
|
|
}
|
|
|
|
|
2014-03-10 09:11:23 -04:00
|
|
|
return &hostConfig
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
hostConfig := &HostConfig{
|
|
|
|
ContainerIDFile: job.Getenv("ContainerIDFile"),
|
move resources from Config to HostConfig
Cgroup resources are host dependent, they should be in hostConfig.
For backward compatibility, we just copy it to hostConfig, and leave it in
Config for now, so there is no regressions, but the right way to use this
throught json is to put it in HostConfig, like:
{
"Hostname": "",
...
"HostConfig": {
"CpuShares": 512,
"Memory": 314572800,
...
}
}
As we will add CpusetMems, CpusetCpus is definitely a better name, but some
users are already using Cpuset in their http APIs, we also make it compatible.
The main idea is keep using Cpuset in Config Struct, and make it has the same
value as CpusetCpus, but not always, some scenarios:
- Users use --cpuset in docker command, it can setup cpuset.cpus and can
get Cpuset field from docker inspect or other http API which will get
config info.
- Users use --cpuset-cpus in docker command, ditto.
- Users use Cpuset field in their http APIs, ditto.
- Users use CpusetCpus field in their http APIs, they won't get Cpuset field
in Config info, because by then, they should already know what happens
to Cpuset.
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
2015-03-10 21:31:18 -04:00
|
|
|
Memory: job.GetenvInt64("Memory"),
|
|
|
|
MemorySwap: job.GetenvInt64("MemorySwap"),
|
|
|
|
CpuShares: job.GetenvInt64("CpuShares"),
|
|
|
|
CpusetCpus: job.Getenv("CpusetCpus"),
|
2014-02-11 23:04:39 -05:00
|
|
|
Privileged: job.GetenvBool("Privileged"),
|
|
|
|
PublishAllPorts: job.GetenvBool("PublishAllPorts"),
|
2014-05-02 19:59:28 -04:00
|
|
|
NetworkMode: NetworkMode(job.Getenv("NetworkMode")),
|
2014-11-10 16:14:17 -05:00
|
|
|
IpcMode: IpcMode(job.Getenv("IpcMode")),
|
2014-11-25 15:10:53 -05:00
|
|
|
PidMode: PidMode(job.Getenv("PidMode")),
|
2015-01-13 16:52:51 -05:00
|
|
|
ReadonlyRootfs: job.GetenvBool("ReadonlyRootfs"),
|
2015-03-16 18:42:15 -04:00
|
|
|
CgroupParent: job.Getenv("CgroupParent"),
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
move resources from Config to HostConfig
Cgroup resources are host dependent, they should be in hostConfig.
For backward compatibility, we just copy it to hostConfig, and leave it in
Config for now, so there is no regressions, but the right way to use this
throught json is to put it in HostConfig, like:
{
"Hostname": "",
...
"HostConfig": {
"CpuShares": 512,
"Memory": 314572800,
...
}
}
As we will add CpusetMems, CpusetCpus is definitely a better name, but some
users are already using Cpuset in their http APIs, we also make it compatible.
The main idea is keep using Cpuset in Config Struct, and make it has the same
value as CpusetCpus, but not always, some scenarios:
- Users use --cpuset in docker command, it can setup cpuset.cpus and can
get Cpuset field from docker inspect or other http API which will get
config info.
- Users use --cpuset-cpus in docker command, ditto.
- Users use Cpuset field in their http APIs, ditto.
- Users use CpusetCpus field in their http APIs, they won't get Cpuset field
in Config info, because by then, they should already know what happens
to Cpuset.
Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
2015-03-10 21:31:18 -04:00
|
|
|
// FIXME: This is for backward compatibility, if people use `Cpuset`
|
|
|
|
// in json, make it workable, we will only pass hostConfig.CpusetCpus
|
|
|
|
// to execDriver.
|
|
|
|
if job.EnvExists("Cpuset") && hostConfig.CpusetCpus == "" {
|
|
|
|
hostConfig.CpusetCpus = job.Getenv("Cpuset")
|
|
|
|
}
|
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
job.GetenvJson("LxcConf", &hostConfig.LxcConf)
|
|
|
|
job.GetenvJson("PortBindings", &hostConfig.PortBindings)
|
2014-05-31 00:00:47 -04:00
|
|
|
job.GetenvJson("Devices", &hostConfig.Devices)
|
2014-08-04 19:14:43 -04:00
|
|
|
job.GetenvJson("RestartPolicy", &hostConfig.RestartPolicy)
|
2015-02-11 14:21:38 -05:00
|
|
|
job.GetenvJson("Ulimits", &hostConfig.Ulimits)
|
2015-02-04 14:04:58 -05:00
|
|
|
job.GetenvJson("LogConfig", &hostConfig.LogConfig)
|
2014-11-03 17:57:18 -05:00
|
|
|
hostConfig.SecurityOpt = job.GetenvList("SecurityOpt")
|
2014-02-11 23:04:39 -05:00
|
|
|
if Binds := job.GetenvList("Binds"); Binds != nil {
|
|
|
|
hostConfig.Binds = Binds
|
|
|
|
}
|
|
|
|
if Links := job.GetenvList("Links"); Links != nil {
|
|
|
|
hostConfig.Links = Links
|
|
|
|
}
|
2014-04-07 22:12:22 -04:00
|
|
|
if Dns := job.GetenvList("Dns"); Dns != nil {
|
|
|
|
hostConfig.Dns = Dns
|
|
|
|
}
|
|
|
|
if DnsSearch := job.GetenvList("DnsSearch"); DnsSearch != nil {
|
|
|
|
hostConfig.DnsSearch = DnsSearch
|
|
|
|
}
|
2014-09-13 00:35:59 -04:00
|
|
|
if ExtraHosts := job.GetenvList("ExtraHosts"); ExtraHosts != nil {
|
|
|
|
hostConfig.ExtraHosts = ExtraHosts
|
|
|
|
}
|
2014-04-08 06:02:17 -04:00
|
|
|
if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
|
|
|
|
hostConfig.VolumesFrom = VolumesFrom
|
|
|
|
}
|
2014-07-10 17:51:15 -04:00
|
|
|
if CapAdd := job.GetenvList("CapAdd"); CapAdd != nil {
|
|
|
|
hostConfig.CapAdd = CapAdd
|
|
|
|
}
|
|
|
|
if CapDrop := job.GetenvList("CapDrop"); CapDrop != nil {
|
|
|
|
hostConfig.CapDrop = CapDrop
|
|
|
|
}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
return hostConfig
|
|
|
|
}
|