Skip to content

Commit a433090

Browse files
committed
Support the containers.conf container_name_as_hostname option
When containers.conf has the "container_name_as_hostname" option set, use that value, with values that don't fit `[A-Za-z0-9][A-Za-z0-9._-]+` stripped out. Signed-off-by: Nalin Dahyabhai <[email protected]>
1 parent 21fe6dc commit a433090

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

run_common.go

+25
Original file line numberDiff line numberDiff line change
@@ -2092,3 +2092,28 @@ func relabel(path, mountLabel string, shared bool) error {
20922092
}
20932093
return nil
20942094
}
2095+
2096+
// mapContainerNameToHostname returns the passed-in string with characters that
2097+
// don't match the pattern "[A-Za-z0-9][A-Za-z0-9._-]+" stripped out.
2098+
func mapContainerNameToHostname(containerName string) string {
2099+
const initialAllowedChars = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"
2100+
const allowedChars = initialAllowedChars + "._-"
2101+
var mapped string
2102+
if containerName != "" {
2103+
mapped += strings.Map(func(r rune) rune {
2104+
if !strings.Contains(initialAllowedChars, string(r)) {
2105+
return -1
2106+
}
2107+
return r
2108+
}, containerName[:1])
2109+
}
2110+
if len(containerName) > 1 {
2111+
mapped += strings.Map(func(r rune) rune {
2112+
if !strings.Contains(allowedChars, string(r)) {
2113+
return -1
2114+
}
2115+
return r
2116+
}, containerName[1:])
2117+
}
2118+
return mapped
2119+
}

run_freebsd.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,17 @@ func (b *Builder) configureNamespaces(g *generate.Generator, options *RunOptions
586586
} else if b.Hostname() != "" {
587587
g.SetHostname(b.Hostname())
588588
} else {
589-
g.SetHostname(stringid.TruncateID(b.ContainerID))
589+
hostname := stringid.TruncateID(b.ContainerID)
590+
defConfig, err := config.Default()
591+
if err != nil {
592+
return false, "", fmt.Errorf("failed to get container config: %w", err)
593+
}
594+
if defConfig.Containers.ContainerNameAsHostName {
595+
if mapped := mapContainerNameToHostname(b.Container); mapped != "" {
596+
hostname = mapped
597+
}
598+
}
599+
g.SetHostname(hostname)
590600
}
591601
} else {
592602
g.SetHostname("")

run_linux.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,17 @@ func (b *Builder) configureNamespaces(g *generate.Generator, options *RunOptions
991991
} else if b.Hostname() != "" {
992992
g.SetHostname(b.Hostname())
993993
} else {
994-
g.SetHostname(stringid.TruncateID(b.ContainerID))
994+
hostname := stringid.TruncateID(b.ContainerID)
995+
defConfig, err := config.Default()
996+
if err != nil {
997+
return false, "", fmt.Errorf("failed to get container config: %w", err)
998+
}
999+
if defConfig.Containers.ContainerNameAsHostName {
1000+
if mapped := mapContainerNameToHostname(b.Container); mapped != "" {
1001+
hostname = mapped
1002+
}
1003+
}
1004+
g.SetHostname(hostname)
9951005
}
9961006
} else {
9971007
g.SetHostname("")

tests/containers.conf

+4
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ default_capabilities = [
6363
default_sysctls = [
6464
"net.ipv4.ping_group_range=0 0",
6565
]
66+
67+
# Use the container's name as its hostname, in preference over its ID.
68+
#
69+
container_name_as_hostname = true

tests/run.bats

+15
Original file line numberDiff line numberDiff line change
@@ -997,3 +997,18 @@ _EOF
997997
run_buildah ? bud --pull=false --layers .
998998
expect_output --substring -- "-c requires an argument"
999999
}
1000+
1001+
@test "container_name_as_hostname" {
1002+
skip_if_no_runtime
1003+
1004+
_prefetch alpine
1005+
1006+
name=alpine-working-container-for-test
1007+
run_buildah from --name :"$name": --cidfile ${TEST_SCRATCH_DIR}/cid alpine
1008+
cid="$output"
1009+
export CONTAINERS_CONF=${TEST_SOURCES}/containers.conf
1010+
run_buildah run $cid hostname
1011+
expect_output "$name"
1012+
run_buildah run $(cat ${TEST_SCRATCH_DIR}/cid) hostname
1013+
expect_output "$name"
1014+
}

0 commit comments

Comments
 (0)