mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #5859 from philips/append-etc-hosts-not-bind
fix(daemon): prepend host /etc/hosts instead of bind mounting
This commit is contained in:
commit
4bb4bf634a
4 changed files with 51 additions and 4 deletions
|
@ -879,9 +879,17 @@ func (container *Container) initializeNetworking() error {
|
||||||
container.Config.Hostname = parts[0]
|
container.Config.Hostname = parts[0]
|
||||||
container.Config.Domainname = parts[1]
|
container.Config.Domainname = parts[1]
|
||||||
}
|
}
|
||||||
container.HostsPath = "/etc/hosts"
|
|
||||||
|
|
||||||
return container.buildHostnameFile()
|
content, err := ioutil.ReadFile("/etc/hosts")
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return container.buildHostnameAndHostsFiles("")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
container.HostsPath = container.getRootResourcePath("hosts")
|
||||||
|
return ioutil.WriteFile(container.HostsPath, content, 0644)
|
||||||
} else if container.hostConfig.NetworkMode.IsContainer() {
|
} else if container.hostConfig.NetworkMode.IsContainer() {
|
||||||
// we need to get the hosts files from the container to join
|
// we need to get the hosts files from the container to join
|
||||||
nc, err := container.getNetworkedContainer()
|
nc, err := container.getNetworkedContainer()
|
||||||
|
|
|
@ -40,8 +40,11 @@ func setupMountsForContainer(container *Container) error {
|
||||||
{container.ResolvConfPath, "/etc/resolv.conf", false, true},
|
{container.ResolvConfPath, "/etc/resolv.conf", false, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
if container.HostnamePath != "" && container.HostsPath != "" {
|
if container.HostnamePath != "" {
|
||||||
mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", false, true})
|
mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", false, true})
|
||||||
|
}
|
||||||
|
|
||||||
|
if container.HostsPath != "" {
|
||||||
mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", false, true})
|
mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", false, true})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,46 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/pkg/iptables"
|
"github.com/dotcloud/docker/pkg/iptables"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestEtcHostsRegularFile(t *testing.T) {
|
||||||
|
runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
|
||||||
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||||
|
errorOut(err, t, out)
|
||||||
|
|
||||||
|
if !strings.HasPrefix(out, "-") {
|
||||||
|
t.Errorf("/etc/hosts should be a regular file")
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteAllContainers()
|
||||||
|
|
||||||
|
logDone("link - /etc/hosts is a regular file")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEtcHostsContentMatch(t *testing.T) {
|
||||||
|
runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
|
||||||
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
||||||
|
errorOut(err, t, out)
|
||||||
|
|
||||||
|
hosts, err := ioutil.ReadFile("/etc/hosts")
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
t.Skip("/etc/hosts does not exist, skip this test")
|
||||||
|
}
|
||||||
|
|
||||||
|
if out != string(hosts) {
|
||||||
|
t.Errorf("container")
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteAllContainers()
|
||||||
|
|
||||||
|
logDone("link - /etc/hosts matches hosts copy")
|
||||||
|
}
|
||||||
|
|
||||||
func TestPingUnlinkedContainers(t *testing.T) {
|
func TestPingUnlinkedContainers(t *testing.T) {
|
||||||
runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
||||||
exitCode, err := runCommand(runCmd)
|
exitCode, err := runCommand(runCmd)
|
||||||
|
|
|
@ -438,7 +438,7 @@ func TestCreateVolume(t *testing.T) {
|
||||||
|
|
||||||
deleteAllContainers()
|
deleteAllContainers()
|
||||||
|
|
||||||
logDone("run - create docker mangaed volume")
|
logDone("run - create docker managed volume")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that creating a volume with a symlink in its path works correctly. Test for #5152.
|
// Test that creating a volume with a symlink in its path works correctly. Test for #5152.
|
||||||
|
|
Loading…
Add table
Reference in a new issue