Skip to content

Commit

Permalink
Make sure /etc/hosts populated correctly with networks
Browse files Browse the repository at this point in the history
The --hostname and containername should always be added to containers.

Added some tests to make sure you can always ping the hostname and container
name from within the container.

Fixes: containers#8095

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Nov 16, 2020
1 parent e593949 commit 4ca4234
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
17 changes: 11 additions & 6 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,14 @@ func (c *Container) makeBindMounts() error {
return err
}
}
} else {
if !c.config.UseImageHosts && c.state.BindMounts["/etc/hosts"] == "" {
newHosts, err := c.generateHosts("/etc/hosts")
if err != nil {
return errors.Wrapf(err, "error creating hosts file for container %s", c.ID())
}
c.state.BindMounts["/etc/hosts"] = newHosts
}
}

// SHM is always added when we mount the container
Expand Down Expand Up @@ -1614,14 +1622,11 @@ func (c *Container) getHosts() string {
}
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)
osHostname, _ := os.Hostname()
hosts += fmt.Sprintf("127.0.1.1 %s %s %s\n", osHostname, c.Hostname(), c.config.Name)
}
if netNone {
hosts += fmt.Sprintf("127.0.1.1 %s\n", c.Hostname())
hosts += fmt.Sprintf("127.0.1.1 %s %s\n", c.Hostname(), c.config.Name)
}
}
}
Expand Down
41 changes: 38 additions & 3 deletions test/e2e/run_networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ var _ = Describe("Podman run networking", func() {
run.WaitWithDefaultTimeout()
Expect(run.ExitCode()).To(BeZero())
Expect(run.OutputToString()).To(ContainSubstring(ipAddr))

create = podmanTest.Podman([]string{"network", "rm", netName})
create.WaitWithDefaultTimeout()
Expect(create.ExitCode()).To(BeZero())
})

It("podman run with new:pod and static-ip", func() {
Expand Down Expand Up @@ -588,14 +592,45 @@ var _ = Describe("Podman run networking", func() {
Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue())
})

It("podman run with --net=none adds hostname to /etc/hosts", func() {
It("podman run with --net=none sets hostname", 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())
})

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

ping_test := func(netns string) {
hostname := "testctr"
run := podmanTest.Podman([]string{"run", netns, "--hostname", hostname, ALPINE, "ping", "-c", "1", hostname})
run.WaitWithDefaultTimeout()
Expect(run.ExitCode()).To(BeZero())

run = podmanTest.Podman([]string{"run", netns, "--hostname", hostname, "--name", "test", ALPINE, "ping", "-c", "1", "test"})
run.WaitWithDefaultTimeout()
Expect(run.ExitCode()).To(BeZero())
}

It("podman attempt to ping container name and hostname --net=none", func() {
ping_test("--net=none")
})

It("podman attempt to ping container name and hostname --net=host", func() {
ping_test("--net=host")
})

It("podman attempt to ping container name and hostname --net=private", func() {
ping_test("--net=private")
})

It("podman run check dnsname plugin", func() {
pod := "testpod"
session := podmanTest.Podman([]string{"pod", "create", "--name", pod})
Expand All @@ -621,10 +656,10 @@ var _ = Describe("Podman run networking", func() {
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())

session = podmanTest.Podman([]string{"run", "--name", "con3", "--pod", pod2, ALPINE, "nslookup", "con3"})
session = podmanTest.Podman([]string{"run", "--name", "con3", "--pod", pod2, ALPINE, "nslookup", "con1"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(1))
Expect(session.ErrorToString()).To(ContainSubstring("can't resolve 'con3'"))
Expect(session.ErrorToString()).To(ContainSubstring("can't resolve 'con1'"))

session = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2})
session.WaitWithDefaultTimeout()
Expand Down

0 comments on commit 4ca4234

Please sign in to comment.