From dc4682be742771c6c3efa74997f26300c9cd5e01 Mon Sep 17 00:00:00 2001 From: Amir Alperin Date: Fri, 9 Jan 2026 21:10:53 +0200 Subject: [PATCH] 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. --- .../Server/SandboxService.swift | 6 ++++- .../Containers/TestCLICreate.swift | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Sources/Services/ContainerSandboxService/Server/SandboxService.swift b/Sources/Services/ContainerSandboxService/Server/SandboxService.swift index add5f836..2517044e 100644 --- a/Sources/Services/ContainerSandboxService/Server/SandboxService.swift +++ b/Sources/Services/ContainerSandboxService/Server/SandboxService.swift @@ -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( diff --git a/Tests/CLITests/Subcommands/Containers/TestCLICreate.swift b/Tests/CLITests/Subcommands/Containers/TestCLICreate.swift index e688e51d..e2d04eb1 100644 --- a/Tests/CLITests/Subcommands/Containers/TestCLICreate.swift +++ b/Tests/CLITests/Subcommands/Containers/TestCLICreate.swift @@ -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)')" + ) + } + } + }