mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-09-25 00:57:58 +00:00
Override hostname for container
Adds the ability to override the container's hostname. Also, uses the first twelve characters of the container ID as the default hostname if none is provided. Signed-off-by: baude <bbaude@redhat.com> Closes: #248 Approved by: baude
This commit is contained in:
parent
946b4ced54
commit
5c3e4cfa62
3 changed files with 34 additions and 0 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,3 +45,12 @@ function setup() {
|
|||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "test addition of hostname" {
|
||||
run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} run --rm --hostname='foobar' ${ALPINE} cat /etc/hostname | grep foobar"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run bash -c "${PODMAN_BINARY} ${PODMAN_OPTIONS} run --rm --hostname='foobar' ${ALPINE} hostname | grep foobar"
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue