diff options
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/container_api.go | 20 | ||||
-rw-r--r-- | libpod/container_internal.go | 5 |
2 files changed, 25 insertions, 0 deletions
diff --git a/libpod/container_api.go b/libpod/container_api.go index be7fd76bc..2b3c83eb2 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -90,6 +90,18 @@ func (c *Container) Init() (err error) { return errors.Wrapf(err, "unable to copy /etc/hosts to container space") } + if c.Spec().Hostname == "" { + id := c.ID() + if len(c.ID()) > 11 { + id = c.ID()[:12] + } + c.config.Spec.Hostname = id + } + runDirHostname, err := c.generateEtcHostname(c.config.Spec.Hostname) + if err != nil { + return errors.Wrapf(err, "unable to generate hostname file for container") + } + // Save OCI spec to disk g := generate.NewFromSpec(c.config.Spec) // If network namespace was requested, add it now @@ -122,6 +134,14 @@ func (c *Container) Init() (err error) { Options: []string{"rw", "bind"}, } g.AddMount(hostsMnt) + // Bind hostname + hostnameMnt := spec.Mount{ + Type: "bind", + Source: runDirHostname, + Destination: "/etc/hostname", + Options: []string{"rw", "bind"}, + } + g.AddMount(hostnameMnt) if c.config.User != "" { if !c.state.Mounted { diff --git a/libpod/container_internal.go b/libpod/container_internal.go index c052f61fe..9b785bfa5 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -508,3 +508,8 @@ func (c *Container) generateHosts() (string, error) { } return c.WriteStringToRundir("hosts", hosts) } + +// generateEtcHostname creates a containers /etc/hostname +func (c *Container) generateEtcHostname(hostname string) (string, error) { + return c.WriteStringToRundir("hostname", hostname) +} |