Add --add-host flag for static /etc/hosts entries#1563
Open
bilby91 wants to merge 1 commit into
Open
Conversation
The container daemon currently builds a minimal `/etc/hosts` containing
only loopback and the container's own primary-interface address — there
is no way for a caller to inject arbitrary host-to-IP mappings.
SandboxService even carries a comment hinting at this:
// NOTE: We can support a user providing new entries eventually, but
// for now craft a default /etc/hosts.
Add a `--add-host host:ip` flag on `container create` / `container run`
(repeatable, matching Docker's flag of the same name and compose's
`extra_hosts`). The flag values flow through `Flags.Management` →
`Parser.extraHosts` → `ContainerConfiguration.extraHosts` → SandboxService,
where each entry is appended to the `/etc/hosts` written before user
processes start.
The parser mirrors the existing `--dns-*` plumbing: separate `@Option`
on the `Management` flag group, validation via `IPAddress` (accepts both
IPv4 and IPv6), splitting on the *first* `:` so that IPv6 addresses
(which themselves contain `:`) parse correctly. DNS hostnames cannot
contain `:`, so the first colon is unambiguous as the separator.
`ContainerConfiguration.extraHosts` is decoded with `decodeIfPresent`
and defaults to `[]`, so configurations encoded by older daemons (or
clients that never set the field) decode unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a
--add-host host:ipflag oncontainer create/container run(repeatable), so callers can inject arbitrary host-to-IP mappings into the container's/etc/hosts. Mirrors Docker's flag of the same name and compose'sextra_hosts.The daemon already builds a minimal
/etc/hosts(loopback + the container's primary-interface address) and the SandboxService even carries a hint at this gap:This PR fills that in by mirroring the existing
--dns-*flag plumbing.Changes
Flags.Managementgains an--add-host@Option(repeatable,host:ipformat).Parser.extraHosts(_:)validates each value viaIPAddress(accepts both IPv4 and IPv6) and splits on the first:— DNS hostnames cannot contain:, so the first colon is unambiguous as the separator and IPv6 addresses (host:2001:db8::1) parse correctly.ContainerConfigurationgains anextraHosts: [ExtraHost]field, decoded withdecodeIfPresentand defaulting to[]so older snapshots decode unchanged.Utility.makeContainerConfigplumbs parsed values into the new field.SandboxServiceappends eachExtraHosttohostsEntriesbefore constructing theHosts(entries:)written into the VM.Motivation
We're building crunchloop/devcontainer — an open-source Go runtime for Dev Containers — and are wiring up
apple/containeras a backend alongside Docker. Several Dev Container / compose features need static host mappings:extra_hostsin compose service definitions.host.docker.internal-style escape hatch).Without
--add-hosttoday, the workaround is tocontainer run→container inspect→container exec --user 0to append to/etc/hostspost-start, which is racy (intra-level peers can resolve each other before either has been patched) and brittle (fails for distroless images that have nosh).The capability is also useful well beyond compose — anything that needs deterministic name resolution without standing up a DNS server: pinning a CI artifact registry by IP, aliasing internal hostnames in air-gapped environments, etc. It's the same role
--add-hostplays in Docker,--dns-searchplays today inapple/container, andextra_hostsplays in compose.Test plan
swift build(full project) — cleanswift test --filter "ParserTest|ContainerConfigurationExtraHostsTests"— 12/12 passhost:2001:db8::1, rejects missing colon, rejects empty hostname / IP, rejects invalid IP--add-host db:192.168.66.2 --add-host cache:192.168.66.3round-trips throughFlags.Management.parseextraHostskey) decodes toextraHosts == []; populated values round-trip through JSON; default-initialized config hasextraHosts == []make fmt— no changesNotes
--add-host host:iponly.host:host-gatewayandhost=ipare deliberately out of scope; either can be added in a follow-up without breaking this surface.apple/container's general "small fixes welcome" guidance in CONTRIBUTING.md). Happy to file an issue first if maintainers prefer.