Fix: Add explicit host entries to container configuration#1340
Fix: Add explicit host entries to container configuration#1340mazdak wants to merge 1 commit intoapple:mainfrom
Conversation
| var hosts = [ContainerConfiguration.HostEntry(ipAddress: "127.0.0.1", hostnames: ["localhost"])] | ||
|
|
||
| if let primaryAddress { | ||
| let ip = String(primaryAddress.split(separator: "/")[0]) |
There was a problem hiding this comment.
Why are we splitting by / here?
public struct IPv4Address {
@inlinable
public var description: String {
"\(bytes[0]).\(bytes[1]).\(bytes[2]).\(bytes[3])"
}
}
There was a problem hiding this comment.
We are splitting by / here because the value coming in as primaryAddress is not a plain IP address — it is a CIDR notation string (e.g. "192.168.1.45/24").
The /24 part is the subnet mask. We only want the actual IP address (192.168.1.45), so we split on the / and take the first part.
Cleaned-up & safer version of that function:
extension SandboxService {
static func resolvedHosts(
hostname: String,
primaryAddress: String?,
extraHosts: [ContainerConfiguration.HostEntry]
) -> [ContainerConfiguration.HostEntry] {
var hosts: [ContainerConfiguration.HostEntry] = [
ContainerConfiguration.HostEntry(ipAddress: "127.0.0.1", hostnames: ["localhost"])
]
if let primaryAddress {
// Split off the CIDR suffix if present (e.g. "192.168.1.45/24" → "192.168.1.45")
let ipOnly = primaryAddress.split(separator: "/").first.map(String.init) ?? primaryAddress
hosts.append(
ContainerConfiguration.HostEntry(
ipAddress: ipOnly,
hostnames: [hostname]
)
)
}
// Add any extra hosts passed in
hosts.append(contentsOf: extraHosts)
return hosts
}
}
There was a problem hiding this comment.
Why are we splitting by
/here?public struct IPv4Address { @inlinable public var description: String { "\(bytes[0]).\(bytes[1]).\(bytes[2]).\(bytes[3])" } }
Also with the way I am running low latency it must be a steady string no breaking if possible.! I can show you diagrams or math that proves it.
|
It'd be good to have a follow up PR that wires this to CLI (e.g., |
Type of Change
Motivation and Context
While building a Docker Compose-like plugin for container and validating it against our real Docker Compose workload, we hit a core limitation: there was no way for callers to ask the runtime to append explicit entries to a container's /etc/hosts.
That showed up most clearly with Compose extra_hosts, especially the common host.docker.internal pattern. The plugin could parse those mappings, but there was no core field to carry them into the sandbox, so the runtime always generated only the default localhost/container-name entries.
In practice, containers that depended on host aliases still failed name resolution even though the compose file specified them.
Why this belongs in core
This is not something the plugin can fake safely. /etc/hosts is generated in the sandbox layer, so callers need a first-class way to provide additional host entries to the runtime.
What this changes
ContainerConfiguration.HostEntryContainerConfiguration.hostsTesting