summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libpod/container_internal_linux.go10
-rw-r--r--pkg/spec/spec.go23
-rw-r--r--test/e2e/run_networking_test.go14
3 files changed, 36 insertions, 11 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go
index 2aba1abde..ba02c9f5a 100644
--- a/libpod/container_internal_linux.go
+++ b/libpod/container_internal_linux.go
@@ -215,8 +215,14 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
g.AddAnnotation(crioAnnotations.Created, c.config.CreatedTime.Format(time.RFC3339Nano))
g.AddAnnotation("org.opencontainers.image.stopSignal", fmt.Sprintf("%d", c.config.StopSignal))
- g.SetHostname(c.Hostname())
- g.AddProcessEnv("HOSTNAME", g.Config.Hostname)
+ for _, i := range c.config.Spec.Linux.Namespaces {
+ if string(i.Type) == spec.UTSNamespace {
+ hostname := c.Hostname()
+ g.SetHostname(hostname)
+ g.AddProcessEnv("HOSTNAME", hostname)
+ break
+ }
+ }
// Only add container environment variable if not already present
foundContainerEnv := false
diff --git a/pkg/spec/spec.go b/pkg/spec/spec.go
index 97305610a..dec3a05ef 100644
--- a/pkg/spec/spec.go
+++ b/pkg/spec/spec.go
@@ -74,18 +74,23 @@ func CreateConfigToOCISpec(config *CreateConfig) (*spec.Spec, error) { //nolint
g.AddAnnotation(key, val)
}
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")
- }
+
+ hostname := config.Hostname
+ if hostname == "" && (config.NetMode.IsHost() || config.UtsMode.IsHost()) {
+ hostname, err = os.Hostname()
+ if err != nil {
+ return nil, errors.Wrap(err, "unable to retrieve hostname")
}
}
- g.SetHostname(config.Hostname)
- if config.Hostname != "" {
- g.AddProcessEnv("HOSTNAME", config.Hostname)
+ g.RemoveHostname()
+ if config.Hostname != "" || !config.UtsMode.IsHost() {
+ // Set the hostname in the OCI configuration only
+ // if specified by the user or if we are creating
+ // a new UTS namespace.
+ g.SetHostname(hostname)
}
+ g.AddProcessEnv("HOSTNAME", hostname)
+
for sysctlKey, sysctlVal := range config.Sysctl {
g.AddLinuxSysctl(sysctlKey, sysctlVal)
}
diff --git a/test/e2e/run_networking_test.go b/test/e2e/run_networking_test.go
index a4bdcdf89..021825d4b 100644
--- a/test/e2e/run_networking_test.go
+++ b/test/e2e/run_networking_test.go
@@ -114,6 +114,20 @@ var _ = Describe("Podman rmi", func() {
match, _ := session.GrepString(hostname)
Expect(match).Should(BeTrue())
})
+ It("podman run --net host --uts host hostname test", func() {
+ session := podmanTest.Podman([]string{"run", "--rm", "--net", "host", "--uts", "host", ALPINE, "printenv", "HOSTNAME"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ match, _ := session.GrepString(hostname)
+ Expect(match).Should(BeTrue())
+ })
+ It("podman run --uts host hostname test", func() {
+ session := podmanTest.Podman([]string{"run", "--rm", "--uts", "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"})