Skip to content

Commit

Permalink
Add hostname to /etc/hosts for --net=none
Browse files Browse the repository at this point in the history
This does not match Docker, which does not add hostname in this
case, but it seems harmless enough.

Fixes containers#8095

Signed-off-by: Matthew Heon <[email protected]>
  • Loading branch information
mheon committed Oct 21, 2020
1 parent a1b942f commit 0864d82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,9 +1550,13 @@ func (c *Container) getHosts() string {
hosts += fmt.Sprintf("# used by slirp4netns\n%s\t%s %s\n", "10.0.2.100", c.Hostname(), c.config.Name)
} else {
hasNetNS := false
netNone := false
for _, ns := range c.config.Spec.Linux.Namespaces {
if ns.Type == spec.NetworkNamespace {
hasNetNS = true
if ns.Path == "" && !c.config.CreateNetNS {
netNone = true
}
break
}
}
Expand All @@ -1564,6 +1568,9 @@ func (c *Container) getHosts() string {
}
hosts += fmt.Sprintf("127.0.1.1 %s\n", osHostname)
}
if netNone {
hosts += fmt.Sprintf("127.0.1.1 %s\n", c.Hostname())
}
}
}
return hosts
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/run_networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,14 @@ var _ = Describe("Podman run networking", func() {
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())
Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue())
})

It("podman run with --net=none adds hostname to /etc/hosts", func() {
hostname := "testctr"
run := podmanTest.Podman([]string{"run", "--net=none", "--hostname", hostname, ALPINE, "hostname"})
run.WaitWithDefaultTimeout()
Expect(run.ExitCode()).To(BeZero())
Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue())
})
})

0 comments on commit 0864d82

Please sign in to comment.