mirror of
https://github.com/containers/podman.git
synced 2025-06-20 17:13:43 +08:00
Merge pull request #8347 from rhatdan/hostname
Make sure /etc/hosts populated correctly with networks
This commit is contained in:
@ -1354,6 +1354,14 @@ func (c *Container) makeBindMounts() error {
|
|||||||
return err
|
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
|
// SHM is always added when we mount the container
|
||||||
@ -1614,14 +1622,11 @@ func (c *Container) getHosts() string {
|
|||||||
}
|
}
|
||||||
if !hasNetNS {
|
if !hasNetNS {
|
||||||
// 127.0.1.1 and host's hostname to match Docker
|
// 127.0.1.1 and host's hostname to match Docker
|
||||||
osHostname, err := os.Hostname()
|
osHostname, _ := os.Hostname()
|
||||||
if err != nil {
|
hosts += fmt.Sprintf("127.0.1.1 %s %s %s\n", osHostname, c.Hostname(), c.config.Name)
|
||||||
osHostname = c.Hostname()
|
|
||||||
}
|
|
||||||
hosts += fmt.Sprintf("127.0.1.1 %s\n", osHostname)
|
|
||||||
}
|
}
|
||||||
if netNone {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -551,6 +551,10 @@ var _ = Describe("Podman run networking", func() {
|
|||||||
run.WaitWithDefaultTimeout()
|
run.WaitWithDefaultTimeout()
|
||||||
Expect(run.ExitCode()).To(BeZero())
|
Expect(run.ExitCode()).To(BeZero())
|
||||||
Expect(run.OutputToString()).To(ContainSubstring(ipAddr))
|
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() {
|
It("podman run with new:pod and static-ip", func() {
|
||||||
@ -588,7 +592,7 @@ var _ = Describe("Podman run networking", func() {
|
|||||||
Expect(strings.Contains(run.OutputToString(), hostname)).To(BeTrue())
|
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"
|
hostname := "testctr"
|
||||||
run := podmanTest.Podman([]string{"run", "--net=none", "--hostname", hostname, ALPINE, "hostname"})
|
run := podmanTest.Podman([]string{"run", "--net=none", "--hostname", hostname, ALPINE, "hostname"})
|
||||||
run.WaitWithDefaultTimeout()
|
run.WaitWithDefaultTimeout()
|
||||||
@ -596,6 +600,37 @@ var _ = Describe("Podman run networking", func() {
|
|||||||
Expect(strings.Contains(run.OutputToString(), hostname)).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, "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() {
|
It("podman run check dnsname plugin", func() {
|
||||||
pod := "testpod"
|
pod := "testpod"
|
||||||
session := podmanTest.Podman([]string{"pod", "create", "--name", pod})
|
session := podmanTest.Podman([]string{"pod", "create", "--name", pod})
|
||||||
@ -621,10 +656,10 @@ var _ = Describe("Podman run networking", func() {
|
|||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session.ExitCode()).To(BeZero())
|
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()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session.ExitCode()).To(Equal(1))
|
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 = podmanTest.Podman([]string{"run", "--name", "con4", "--network", net, ALPINE, "nslookup", pod2})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
|
Reference in New Issue
Block a user