Add ExtraHosts to Docker driver

This PR allows job submitters to add extra hosts to the containers
/etc/hosts file.

Fixes https://github.com/hashicorp/nomad/issues/2546
This commit is contained in:
Alex Dadgar 2017-04-11 10:52:24 -07:00
parent 30ab0e8ffc
commit cdd624ff5b
2 changed files with 11 additions and 4 deletions

View file

@ -146,6 +146,7 @@ type DockerDriverConfig struct {
Privileged bool `mapstructure:"privileged"` // Flag to run the container in privileged mode
DNSServers []string `mapstructure:"dns_servers"` // DNS Server for containers
DNSSearchDomains []string `mapstructure:"dns_search_domains"` // DNS Search domains for containers
ExtraHosts []string `mapstructure:"extra_hosts"` // Add host to /etc/hosts (host:IP)
Hostname string `mapstructure:"hostname"` // Hostname for containers
LabelsRaw []map[string]string `mapstructure:"labels"` //
Labels map[string]string `mapstructure:"-"` // Labels to set when the container starts up
@ -200,6 +201,7 @@ func NewDockerDriverConfig(task *structs.Task, env *env.TaskEnvironment) (*Docke
dconf.VolumeDriver = env.ReplaceEnv(dconf.VolumeDriver)
dconf.DNSServers = env.ParseAndReplace(dconf.DNSServers)
dconf.DNSSearchDomains = env.ParseAndReplace(dconf.DNSSearchDomains)
dconf.ExtraHosts = env.ParseAndReplace(dconf.ExtraHosts)
for _, m := range dconf.LabelsRaw {
for k, v := range m {
@ -372,6 +374,9 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error {
"dns_search_domains": &fields.FieldSchema{
Type: fields.TypeArray,
},
"extra_hosts": &fields.FieldSchema{
Type: fields.TypeArray,
},
"hostname": &fields.FieldSchema{
Type: fields.TypeString,
},
@ -818,10 +823,9 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas
}
}
// set DNS search domains
for _, domain := range driverConfig.DNSSearchDomains {
hostConfig.DNSSearch = append(hostConfig.DNSSearch, domain)
}
// set DNS search domains and extra hosts
hostConfig.DNSSearch = driverConfig.DNSSearchDomains
hostConfig.ExtraHosts = driverConfig.ExtraHosts
hostConfig.IpcMode = driverConfig.IpcMode
hostConfig.PidMode = driverConfig.PidMode

View file

@ -160,6 +160,9 @@ The `docker` driver supports the following configuration in the job spec:
* `dns_search_domains` - (Optional) A list of DNS search domains for the container
to use.
* `extra_hosts` - (Optional) A list of hosts, given as host:IP, to be added to
`/etc/hosts`.
* `SSL` - (Optional) If this is set to true, Nomad uses SSL to talk to the
repository. The default value is `true`. **Deprecated as of 0.5.3**