Skip to content

Commit

Permalink
Add ExtraHosts to Docker driver
Browse files Browse the repository at this point in the history
This PR allows job submitters to add extra hosts to the containers
/etc/hosts file.

Fixes #2546
  • Loading branch information
dadgar committed Apr 11, 2017
1 parent bca74f2 commit 8d07bd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand 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 {
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down

0 comments on commit 8d07bd8

Please sign in to comment.