Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hostname to /etc/hosts for --net=none #8101

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this test supposed to accomplish? Actually, what exactly is this PR supposed to accomplish? As I read the description and the code -- and of course I may just be confused -- I expected the given hostname to appear in the container's /etc/hosts file. That is not what I'm seeing:

$ ./bin/podman run --net=none --hostname abcde alpine  cat /etc/hosts
127.0.0.1       localhost localhost.localdomain
::1             localhost localhost.localdomain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a bug, because it should be in there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, none of the code added in this PR is ever actually invoked, because of:

if !netDisabled {

(This is what calls generateHosts(), and it's not called if netDisabled).

})
})