From cdd624ff5b8757632b33bf42ae1a892e3e2c6d59 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 11 Apr 2017 10:52:24 -0700 Subject: [PATCH] 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 --- client/driver/docker.go | 12 ++++++++---- website/source/docs/drivers/docker.html.md | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index 60ab5385f..b045e3f3b 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -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 diff --git a/website/source/docs/drivers/docker.html.md b/website/source/docs/drivers/docker.html.md index b6adf0470..093c77dcc 100644 --- a/website/source/docs/drivers/docker.html.md +++ b/website/source/docs/drivers/docker.html.md @@ -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**