summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@pm.me>2020-10-19 15:25:06 -0400
committerMatthew Heon <matthew.heon@pm.me>2020-10-20 10:31:15 -0400
commit1b288a35ba931c0b524af550749a7c1fd48b452f (patch)
treec13115fc7f40591e317f6025c5be608cdcd2c900
parent9f98b3447c7a1c34d328a387993722e4e7113cd2 (diff)
downloadpodman-1b288a35ba931c0b524af550749a7c1fd48b452f.tar.gz
podman-1b288a35ba931c0b524af550749a7c1fd48b452f.tar.bz2
podman-1b288a35ba931c0b524af550749a7c1fd48b452f.zip
Ensure that hostname is added to hosts with net=host
When a container uses --net=host the default hostname is set to the host's hostname. However, we were not creating any entries in `/etc/hosts` despite having a hostname, which is incorrect. This hostname, for Docker compat, will always be the hostname of the host system, not the container, and will be assigned to IP 127.0.1.1 (not the standard localhost address). Also, when `--hostname` and `--net=host` are both passed, still use the hostname from `--hostname`, not the host's hostname (we still use the host's hostname by default in this case if the `--hostname` flag is not passed). Fixes #8054 Signed-off-by: Matthew Heon <matthew.heon@pm.me>
-rw-r--r--libpod/container_internal_linux.go28
-rw-r--r--pkg/specgen/generate/namespaces.go2
-rw-r--r--test/e2e/run_networking_test.go15
3 files changed, 40 insertions, 5 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go
index eff390e46..ffb2f5b73 100644
--- a/libpod/container_internal_linux.go
+++ b/libpod/container_internal_linux.go
@@ -1541,11 +1541,31 @@ func (c *Container) getHosts() string {
}
}
- if c.config.NetMode.IsSlirp4netns() {
- // When using slirp4netns, the interface gets a static IP
- hosts += fmt.Sprintf("# used by slirp4netns\n%s\t%s %s\n", "10.0.2.100", c.Hostname(), c.Config().Name)
- }
hosts += c.cniHosts()
+
+ // If not making a network namespace, add our own hostname.
+ if c.Hostname() != "" {
+ if c.config.NetMode.IsSlirp4netns() {
+ // When using slirp4netns, the interface gets a static IP
+ hosts += fmt.Sprintf("# used by slirp4netns\n%s\t%s %s\n", "10.0.2.100", c.Hostname(), c.config.Name)
+ } else {
+ hasNetNS := false
+ for _, ns := range c.config.Spec.Linux.Namespaces {
+ if ns.Type == spec.NetworkNamespace {
+ hasNetNS = true
+ break
+ }
+ }
+ if !hasNetNS {
+ // 127.0.1.1 and host's hostname to match Docker
+ osHostname, err := os.Hostname()
+ if err != nil {
+ osHostname = c.Hostname()
+ }
+ hosts += fmt.Sprintf("127.0.1.1 %s\n", osHostname)
+ }
+ }
+ }
return hosts
}
diff --git a/pkg/specgen/generate/namespaces.go b/pkg/specgen/generate/namespaces.go
index 7adb8be6a..7e4f09dc4 100644
--- a/pkg/specgen/generate/namespaces.go
+++ b/pkg/specgen/generate/namespaces.go
@@ -342,7 +342,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
return errors.Wrapf(err, "error looking up container to share uts namespace with")
}
hostname = utsCtr.Hostname()
- case s.NetNS.NSMode == specgen.Host || s.UtsNS.NSMode == specgen.Host:
+ case (s.NetNS.NSMode == specgen.Host && hostname == "") || s.UtsNS.NSMode == specgen.Host:
tmpHostname, err := os.Hostname()
if err != nil {
return errors.Wrap(err, "unable to retrieve hostname of the host")
diff --git a/test/e2e/run_networking_test.go b/test/e2e/run_networking_test.go
index e14482db7..540ac5409 100644
--- a/test/e2e/run_networking_test.go
+++ b/test/e2e/run_networking_test.go
@@ -571,4 +571,19 @@ var _ = Describe("Podman run networking", func() {
podrm.WaitWithDefaultTimeout()
Expect(podrm.ExitCode()).To(BeZero())
})
+
+ It("podman run net=host adds entry to /etc/hosts", func() {
+ run := podmanTest.Podman([]string{"run", "--net=host", ALPINE, "cat", "/etc/hosts"})
+ run.WaitWithDefaultTimeout()
+ Expect(run.ExitCode()).To(BeZero())
+ Expect(strings.Contains(run.OutputToString(), "127.0.1.1")).To(BeTrue())
+ })
+
+ It("podman run with --net=host and --hostname sets correct hostname", func() {
+ hostname := "testctr"
+ run := podmanTest.Podman([]string{"run", "--net=host", "--hostname", hostname, ALPINE, "hostname"})
+ run.WaitWithDefaultTimeout()
+ Expect(run.ExitCode()).To(BeZero())
+ Expect(strings.Contains(run.OutputToString(), "testctr")).To(BeTrue())
+ })
})