Skip to content

Commit

Permalink
Merge pull request containers#3586 from rhatdan/docs
Browse files Browse the repository at this point in the history
Add support for host.containers.internal in the /etc/hosts
  • Loading branch information
openshift-merge-robot authored Oct 14, 2021
2 parents b4c5e93 + f8c1526 commit e4a4f2c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
24 changes: 21 additions & 3 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,29 @@ func (b *Builder) generateHosts(rdir, hostname string, addHosts []string, chownO
}
hosts.Write([]byte(fmt.Sprintf("%s\t%s\n", values[1], values[0])))
}
hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s %s\n", b.Container, hostname)))
hosts.Write([]byte(fmt.Sprintf("::1 %s %s\n", b.Container, hostname)))

if hostname != "" {
hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s\n", hostname)))
hosts.Write([]byte(fmt.Sprintf("::1 %s\n", hostname)))
// getLocalIP returns the non loopback local IP of the host
getLocalIP := func() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
if ip := getLocalIP(); ip != "" {
hosts.Write([]byte(fmt.Sprintf("%s %s\n", ip, "host.containers.internal")))
}

cfile := filepath.Join(rdir, filepath.Base(hostPath))
if err = ioutils.AtomicWriteFile(cfile, hosts.Bytes(), stat.Mode().Perm()); err != nil {
return "", errors.Wrapf(err, "error writing /etc/hosts into the container")
Expand Down
15 changes: 11 additions & 4 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,16 @@ function configure_and_check_user() {

run_buildah from --quiet --pull=false --signature-policy ${TESTSDIR}/policy.json debian
cid=$output
run_buildah 125 run --isolation=chroot --network=bogus $cid cat /etc/hosts
run_buildah 125 run --network=bogus $cid cat /etc/hosts
expect_output "checking network namespace: stat bogus: no such file or directory"
run_buildah run --isolation=chroot --network=container $cid cat /etc/hosts

run_buildah run $cid cat /etc/hosts
expect_output --substring "127.0.0.1.*$cid"
expect_output --substring "::1.*$cid"
ip=$(hostname -I | cut -f 1 -d " ")
expect_output --substring "$ip.*host.containers.internal"

run_buildah run --network=container $cid cat /etc/hosts
expect_output --substring "# Generated by Buildah"
m=$(buildah mount $cid)
run cat $m/etc/hosts
Expand All @@ -538,7 +545,7 @@ function configure_and_check_user() {

run_buildah from --quiet --pull=false --signature-policy ${TESTSDIR}/policy.json debian
cid=$output
run_buildah run --isolation=chroot --network=host $cid cat /etc/hosts
run_buildah run --network=host $cid cat /etc/hosts
expect_output --substring "# Generated by Buildah"
m=$(buildah mount $cid)
run cat $m/etc/hosts
Expand All @@ -548,7 +555,7 @@ function configure_and_check_user() {

run_buildah from --quiet --pull=false --signature-policy ${TESTSDIR}/policy.json debian
cid=$output
run_buildah run --isolation=chroot --network=none $cid sh -c 'echo "110.110.110.0 fake_host" >> /etc/hosts; cat /etc/hosts'
run_buildah run --network=none $cid sh -c 'echo "110.110.110.0 fake_host" >> /etc/hosts; cat /etc/hosts'
expect_output "110.110.110.0 fake_host"
m=$(buildah mount $cid)
run cat $m/etc/hosts
Expand Down

0 comments on commit e4a4f2c

Please sign in to comment.