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

Only add 127.0.0.1 entry to /etc/hosts with --net=none #11605

Merged
merged 1 commit into from
Sep 16, 2021
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
13 changes: 7 additions & 6 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2033,15 +2033,16 @@ func (c *Container) getHosts() string {

// Do we have a network namespace?
netNone := false
for _, ns := range c.config.Spec.Linux.Namespaces {
if ns.Type == spec.NetworkNamespace {
if ns.Path == "" && !c.config.CreateNetNS {
netNone = true
if c.config.NetNsCtr == "" && !c.config.CreateNetNS {
Copy link
Member

Choose a reason for hiding this comment

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

Honestly, this is probably safer...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes we should fix this for the future so that we store the netmode in libpod. But now we are stucked with this forever if we want to support backwards compat.

for _, ns := range c.config.Spec.Linux.Namespaces {
if ns.Type == spec.NetworkNamespace {
if ns.Path == "" {
netNone = true
}
break
}
break
}
}

// If we are net=none (have a network namespace, but not connected to
// anything) add the container's name and hostname to localhost.
if netNone {
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/run_networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,18 @@ var _ = Describe("Podman run networking", func() {
Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue())
})

It("podman run with pod does not add extra 127 entry to /etc/hosts", func() {
pod := "testpod"
hostname := "test-hostname"
run := podmanTest.Podman([]string{"pod", "create", "--hostname", hostname, "--name", pod})
run.WaitWithDefaultTimeout()
Expect(run).Should(Exit(0))
run = podmanTest.Podman([]string{"run", "--pod", pod, ALPINE, "cat", "/etc/hosts"})
run.WaitWithDefaultTimeout()
Expect(run).Should(Exit(0))
Expect(run.OutputToString()).ToNot(ContainSubstring("127.0.0.1 %s", hostname))
})

ping_test := func(netns string) {
hostname := "testctr"
run := podmanTest.Podman([]string{"run", netns, "--hostname", hostname, ALPINE, "ping", "-c", "1", hostname})
Expand Down