Skip to content

Commit

Permalink
docker: move host path for hosts file mount to alloc dir
Browse files Browse the repository at this point in the history
In Nomad 1.1.1 we generate a hosts file based on the Nomad-owned network
namespace, rather than using the default hosts file from the pause
container. This hosts file should be shared between tasks in the same
allocation so that tasks can update the file and have the results propagated
between tasks.
  • Loading branch information
tgross committed Jun 28, 2021
1 parent 2cd8f3a commit 2bd0815
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
// that comes from the pause container
if task.NetworkIsolation != nil && driverConfig.NetworkMode == "" {
etcHostMount, err := hostnames.GenerateEtcHostsMount(
task.TaskDir().Dir, task.NetworkIsolation, driverConfig.ExtraHosts)
task.AllocDir, task.NetworkIsolation, driverConfig.ExtraHosts)
if err != nil {
return c, fmt.Errorf("failed to build mount for /etc/hosts: %v", err)
}
Expand Down
12 changes: 9 additions & 3 deletions drivers/shared/hostnames/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -56,9 +57,14 @@ ff02::3 ip6-allhosts
}

path := filepath.Join(taskDir, "hosts")
err := ioutil.WriteFile(path, []byte(content.String()), 0644)
if err != nil {
return nil, err

// tasks within an alloc should be able to share and modify the file, so
// only write to it if it doesn't exist
if _, err := os.Stat(path); os.IsNotExist(err) {
err := ioutil.WriteFile(path, []byte(content.String()), 0644)
if err != nil {
return nil, err
}
}

// Note that we're not setting readonly. The file is in the task dir
Expand Down

0 comments on commit 2bd0815

Please sign in to comment.