Merge pull request #2535 from clinta/docker-ip

Allow specifying container IP with docker driver
This commit is contained in:
Alex Dadgar 2017-04-07 12:59:12 -07:00 committed by GitHub
commit 81188906a5
2 changed files with 30 additions and 4 deletions

View file

@ -136,6 +136,8 @@ type DockerDriverConfig struct {
IpcMode string `mapstructure:"ipc_mode"` // The IPC mode of the container - host and none IpcMode string `mapstructure:"ipc_mode"` // The IPC mode of the container - host and none
NetworkMode string `mapstructure:"network_mode"` // The network mode of the container - host, nat and none NetworkMode string `mapstructure:"network_mode"` // The network mode of the container - host, nat and none
NetworkAliases []string `mapstructure:"network_aliases"` // The network-scoped alias for the container NetworkAliases []string `mapstructure:"network_aliases"` // The network-scoped alias for the container
IPv4Address string `mapstructure:"ipv4_address"` // The container ipv4 address
IPv6Address string `mapstructure:"ipv6_address"` // the container ipv6 address
PidMode string `mapstructure:"pid_mode"` // The PID mode of the container - host and none PidMode string `mapstructure:"pid_mode"` // The PID mode of the container - host and none
UTSMode string `mapstructure:"uts_mode"` // The UTS mode of the container - host and none UTSMode string `mapstructure:"uts_mode"` // The UTS mode of the container - host and none
UsernsMode string `mapstructure:"userns_mode"` // The User namespace mode of the container - host and none UsernsMode string `mapstructure:"userns_mode"` // The User namespace mode of the container - host and none
@ -187,6 +189,8 @@ func NewDockerDriverConfig(task *structs.Task, env *env.TaskEnvironment) (*Docke
dconf.IpcMode = env.ReplaceEnv(dconf.IpcMode) dconf.IpcMode = env.ReplaceEnv(dconf.IpcMode)
dconf.NetworkMode = env.ReplaceEnv(dconf.NetworkMode) dconf.NetworkMode = env.ReplaceEnv(dconf.NetworkMode)
dconf.NetworkAliases = env.ParseAndReplace(dconf.NetworkAliases) dconf.NetworkAliases = env.ParseAndReplace(dconf.NetworkAliases)
dconf.IPv4Address = env.ReplaceEnv(dconf.IPv4Address)
dconf.IPv6Address = env.ReplaceEnv(dconf.IPv6Address)
dconf.PidMode = env.ReplaceEnv(dconf.PidMode) dconf.PidMode = env.ReplaceEnv(dconf.PidMode)
dconf.UTSMode = env.ReplaceEnv(dconf.UTSMode) dconf.UTSMode = env.ReplaceEnv(dconf.UTSMode)
dconf.Hostname = env.ReplaceEnv(dconf.Hostname) dconf.Hostname = env.ReplaceEnv(dconf.Hostname)
@ -341,6 +345,12 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error {
"network_aliases": &fields.FieldSchema{ "network_aliases": &fields.FieldSchema{
Type: fields.TypeArray, Type: fields.TypeArray,
}, },
"ipv4_address": &fields.FieldSchema{
Type: fields.TypeString,
},
"ipv6_address": &fields.FieldSchema{
Type: fields.TypeString,
},
"pid_mode": &fields.FieldSchema{ "pid_mode": &fields.FieldSchema{
Type: fields.TypeString, Type: fields.TypeString,
}, },
@ -916,19 +926,29 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas
d.logger.Printf("[DEBUG] driver.docker: setting container name to: %s", containerName) d.logger.Printf("[DEBUG] driver.docker: setting container name to: %s", containerName)
var networkingConfig *docker.NetworkingConfig var networkingConfig *docker.NetworkingConfig
if len(driverConfig.NetworkAliases) > 0 { if len(driverConfig.NetworkAliases) > 0 || driverConfig.IPv4Address != "" || driverConfig.IPv6Address != "" {
networkingConfig = &docker.NetworkingConfig{ networkingConfig = &docker.NetworkingConfig{
EndpointsConfig: map[string]*docker.EndpointConfig{ EndpointsConfig: map[string]*docker.EndpointConfig{
hostConfig.NetworkMode: &docker.EndpointConfig{ hostConfig.NetworkMode: &docker.EndpointConfig{},
Aliases: driverConfig.NetworkAliases,
},
}, },
} }
}
if len(driverConfig.NetworkAliases) > 0 {
networkingConfig.EndpointsConfig[hostConfig.NetworkMode].Aliases = driverConfig.NetworkAliases
d.logger.Printf("[DEBUG] driver.docker: using network_mode %q with network aliases: %v", d.logger.Printf("[DEBUG] driver.docker: using network_mode %q with network aliases: %v",
hostConfig.NetworkMode, strings.Join(driverConfig.NetworkAliases, ", ")) hostConfig.NetworkMode, strings.Join(driverConfig.NetworkAliases, ", "))
} }
if driverConfig.IPv4Address != "" || driverConfig.IPv6Address != "" {
networkingConfig.EndpointsConfig[hostConfig.NetworkMode].IPAMConfig = &docker.EndpointIPAMConfig{
IPv4Address: driverConfig.IPv4Address,
IPv6Address: driverConfig.IPv6Address,
}
d.logger.Printf("[DEBUG] driver.docker: using network_mode %q with ipv4: %q and ipv6: %q",
hostConfig.NetworkMode, driverConfig.IPv4Address, driverConfig.IPv6Address)
}
return docker.CreateContainerOptions{ return docker.CreateContainerOptions{
Name: containerName, Name: containerName,
Config: config, Config: config,

View file

@ -144,6 +144,12 @@ The `docker` driver supports the following configuration in the job spec:
} }
``` ```
* `ipv4_address` - (Optional) The IPv4 address to be used for the container.
Requires docker 1.13.0. Only works with user defined networks.
* `ipv6_address` - (Optional) The IPv6 address to be used for the container.
Requires docker 1.13.0. Only works with user defined networks.
* `hostname` - (Optional) The hostname to assign to the container. When * `hostname` - (Optional) The hostname to assign to the container. When
launching more than one of a task (using `count`) with this option set, every launching more than one of a task (using `count`) with this option set, every
container the task starts will have the same hostname. container the task starts will have the same hostname.