Fix handling of hostname in --net=host

Hostname should be set to the hosts hostname when network is none.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>

Closes: #1274
Approved by: giuseppe
This commit is contained in:
Daniel J Walsh
2018-08-14 14:59:24 -04:00
committed by Atomic Bot
parent 77baae25ce
commit cd7102a70e
2 changed files with 44 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package createconfig package createconfig
import ( import (
"os"
"strings" "strings"
"github.com/docker/docker/daemon/caps" "github.com/docker/docker/daemon/caps"
@ -73,6 +74,14 @@ func CreateConfigToOCISpec(config *CreateConfig) (*spec.Spec, error) { //nolint
g.AddAnnotation(key, val) g.AddAnnotation(key, val)
} }
g.SetRootReadonly(config.ReadOnlyRootfs) g.SetRootReadonly(config.ReadOnlyRootfs)
if config.Hostname == "" {
if config.NetMode.IsHost() {
config.Hostname, err = os.Hostname()
if err != nil {
return nil, errors.Wrap(err, "unable to retrieve hostname")
}
}
}
g.SetHostname(config.Hostname) g.SetHostname(config.Hostname)
if config.Hostname != "" { if config.Hostname != "" {
g.AddProcessEnv("HOSTNAME", config.Hostname) g.AddProcessEnv("HOSTNAME", config.Hostname)

View File

@ -13,6 +13,7 @@ var _ = Describe("Podman rmi", func() {
tempdir string tempdir string
err error err error
podmanTest PodmanTest podmanTest PodmanTest
hostname, _ = os.Hostname()
) )
BeforeEach(func() { BeforeEach(func() {
@ -98,4 +99,35 @@ var _ = Describe("Podman rmi", func() {
Expect(containerConfig[0].NetworkSettings.Ports[0].HostPort).ToNot(Equal("80")) Expect(containerConfig[0].NetworkSettings.Ports[0].HostPort).ToNot(Equal("80"))
}) })
It("podman run hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", ALPINE, "printenv", "HOSTNAME"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
match, _ := session.GrepString(hostname)
Expect(match).Should(BeFalse())
})
It("podman run --net host hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", ALPINE, "printenv", "HOSTNAME"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
match, _ := session.GrepString(hostname)
Expect(match).Should(BeTrue())
})
It("podman run --net host --hostname ... hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
match, _ := session.GrepString("foobar")
Expect(match).Should(BeTrue())
})
It("podman run --hostname ... hostname test", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--hostname", "foobar", ALPINE, "printenv", "HOSTNAME"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
match, _ := session.GrepString("foobar")
Expect(match).Should(BeTrue())
})
}) })