Skip to content

Fix: Add explicit host entries to container configuration#1340

Open
mazdak wants to merge 1 commit intoapple:mainfrom
mazdak:pr2/container-host-entries
Open

Fix: Add explicit host entries to container configuration#1340
mazdak wants to merge 1 commit intoapple:mainfrom
mazdak:pr2/container-host-entries

Conversation

@mazdak
Copy link
Copy Markdown
Contributor

@mazdak mazdak commented Mar 22, 2026

Type of Change

  • Bug fix

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

  • add ContainerConfiguration.HostEntry
  • add ContainerConfiguration.hosts
  • preserve those entries through configuration encoding/decoding
  • extend sandbox host generation to append caller-provided host entries after the default localhost and primary hostname entries
  • add focused tests for round-tripping and host resolution behavior

Testing

  • Tested locally
  • Added/updated tests

var hosts = [ContainerConfiguration.HostEntry(ipAddress: "127.0.0.1", hostnames: ["localhost"])]

if let primaryAddress {
let ip = String(primaryAddress.split(separator: "/")[0])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we splitting by / here?

public struct IPv4Address {
    @inlinable
    public var description: String {
        "\(bytes[0]).\(bytes[1]).\(bytes[2]).\(bytes[3])"
    }
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JaewonHur
Copy link
Copy Markdown
Contributor

It'd be good to have a follow up PR that wires this to CLI (e.g., --add-host [name:ip] in Docker).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants