fix: extract hostname from FQDN (#1011) (#1017)

- Set the container hostname to the first DNS
  label derived from the container id, strip everything
  after the first dot.
- Fixes #1011.
This commit is contained in:
Amir Alperin
2026-01-09 11:10:53 -08:00
committed by GitHub
parent 4af1cc01c4
commit dc4682be74
2 changed files with 30 additions and 1 deletions
@@ -849,7 +849,11 @@ public actor SandboxService {
czConfig.sockets.append(socketConfig)
}
czConfig.hostname = config.id
let containerId = config.id
czConfig.hostname =
containerId.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: true)
.first
.map { String($0) } ?? containerId
if let dns = config.dns {
czConfig.dns = DNS(
@@ -97,4 +97,29 @@ class TestCLICreateCommand: CLITest {
}
}
@Test func testCreateWithFQDNName() throws {
let name = "test.example.com"
let expectedHostname = "test"
#expect(throws: Never.self, "expected container create with FQDN name to succeed") {
try doCreate(name: name)
try doStart(name: name)
defer {
try? doStop(name: name)
try? doRemove(name: name)
}
try waitForContainerRunning(name)
let inspectResp = try inspectContainer(name)
let attachmentHostname = inspectResp.networks.first?.hostname ?? ""
let gotHostname =
attachmentHostname
.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: true)
.first
.map { String($0) } ?? attachmentHostname
#expect(
gotHostname == expectedHostname,
"expected hostname to be extracted as '\(expectedHostname)' from FQDN '\(name)', got '\(gotHostname)' (attachment hostname: '\(attachmentHostname)')"
)
}
}
}