mirror of
https://github.com/apple/container.git
synced 2026-09-22 15:45:38 +00:00
Generate /etc/hosts by default (#423)
Closes: #314 This change uses the new `.hosts` config entry on Containerization's LinuxContainer config to setup a default /etc/hosts file in the container. Today the two entries are localhost to 127.0.0.1 and the container's IP to its hostname.
This commit is contained in:
@@ -167,6 +167,19 @@ public actor SandboxService {
|
||||
czConfig.process.stdout = stdout
|
||||
czConfig.process.stderr = stderr
|
||||
czConfig.process.stdin = stdin
|
||||
// NOTE: We can support a user providing new entries eventually, but for now craft
|
||||
// a default /etc/hosts.
|
||||
var hostsEntries = [Hosts.Entry.localHostIPV4()]
|
||||
if !interfaces.isEmpty {
|
||||
let primaryIfaceAddr = interfaces[0].address
|
||||
let ip = primaryIfaceAddr.split(separator: "/")
|
||||
hostsEntries.append(
|
||||
Hosts.Entry(
|
||||
ipAddress: String(ip[0]),
|
||||
hostnames: [czConfig.hostname],
|
||||
))
|
||||
}
|
||||
czConfig.hosts = Hosts(entries: hostsEntries)
|
||||
}
|
||||
|
||||
await self.setContainer(
|
||||
|
||||
@@ -408,6 +408,35 @@ class TestCLIRunCommand: CLITest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test func testRunDefaultHostsEntries() throws {
|
||||
do {
|
||||
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
|
||||
try doLongRun(name: name)
|
||||
defer {
|
||||
try? doStop(name: name)
|
||||
}
|
||||
|
||||
let inspectOutput = try inspectContainer(name)
|
||||
let ip = String(inspectOutput.networks[0].address.split(separator: "/")[0])
|
||||
|
||||
let output = try doExec(name: name, cmd: ["cat", "/etc/hosts"])
|
||||
let lines = output.split(separator: "\n")
|
||||
|
||||
let expectedEntries = [("127.0.0.1", "localhost"), (ip, name)]
|
||||
|
||||
for (i, line) in lines.enumerated() {
|
||||
let words = line.split(separator: " ").map { String($0) }
|
||||
#expect(words.count >= 2, "expected /etc/hosts entry to have 2 or more entries")
|
||||
let expected = expectedEntries[i]
|
||||
#expect(expected.0 == words[0], "expected /etc/hosts entries IP to be \(expected.0), instead got \(words[0])")
|
||||
#expect(expected.1 == words[1], "expected /etc/hosts entries host to be \(expected.1), instead got \(words[1])")
|
||||
}
|
||||
} catch {
|
||||
Issue.record("failed to run container \(error)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@Test func testRunCommandDNSOption() throws {
|
||||
do {
|
||||
let name: String! = Test.current?.name.trimmingCharacters(in: ["(", ")"])
|
||||
|
||||
Reference in New Issue
Block a user