-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker: generate /etc/hosts file for bridge network mode
When `network.mode = "bridge"`, we create a pause container in Docker with no networking so that we have a process to hold the network namespace we create in Nomad. The default `/etc/hosts` file of that pause container is then used for all the Docker tasks that share that network namespace. Some applications rely on this file being populated. This changeset generates a `/etc/hosts` file and bind-mounts it to the container when Nomad owns the network, so that the container's hostname has an IP in the file as expected.
- Loading branch information
Showing
7 changed files
with
437 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package hostnames | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/hashicorp/nomad/plugins/drivers" | ||
) | ||
|
||
func GenerateEtcHostsMount(taskDir string, conf *drivers.NetworkIsolationSpec) (*drivers.MountConfig, error) { | ||
if conf == nil || conf.Mode != drivers.NetIsolationModeGroup { | ||
return nil, nil | ||
} | ||
hostsCfg := conf.HostsConfig | ||
if hostsCfg == nil || hostsCfg.Address == "" || hostsCfg.Hostname == "" { | ||
return nil, nil | ||
} | ||
|
||
content := fmt.Sprintf(`# this file was generated by Nomad | ||
127.0.0.1 localhost | ||
::1 ip6-localhost ip6-loopback | ||
fe00::0 ip6-localnet | ||
ff00::0 ip6-mcastprefix | ||
ff02::1 ip6-allnodes | ||
ff02::2 ip6-allrouters | ||
ff02::3 ip6-allhosts | ||
# this entry is the IP address and hostname of the allocation | ||
# shared among all tasks in the task group's network | ||
%s %s | ||
`, hostsCfg.Address, hostsCfg.Hostname) | ||
|
||
path := filepath.Join(taskDir, "hosts") | ||
err := ioutil.WriteFile(path, []byte(content), 0755) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mount := &drivers.MountConfig{ | ||
TaskPath: "/etc/hosts", | ||
HostPath: path, | ||
Readonly: true, | ||
PropagationMode: "private", | ||
} | ||
|
||
return mount, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.