compose/pkg/e2e/testdata/TestPreStartHookExtraHosts/compose.yaml
Nicolas De Loof ef277d266b pre_start: inherit the whole container specification generically
pre_start hooks carry the full container specification, but the
init-container runner only consumed a handful of attributes (image,
command, user, env, workdir) — everything else was silently dropped.

Instead of wiring attributes one by one, the hook's specification is
merged over the service's ContainerSpec through compose-go's own file
merge machinery (override.Merge on the canonical yaml tree): command
and entrypoint replace, environment merges per key with the hook
winning, extra_hosts and dns accumulate entries, ulimits merge — the
exact per-attribute rules of multi-file compose, maintained in one
place. Every ContainerSpec attribute inherits this way, current and
future, with zero attribute-specific code.

The merged spec then runs through the standard create path
(getCreateConfigs) as a service-shaped one-off, so resources,
capabilities, dns, sysctls, logging... materialize exactly as they
would for a service container. Hook containers keep their minimal
labels and carry no container-number, so tooling telling replicas
apart does not count them.

The only deliberate exception is volumes: mounts inherit at runtime
through volumes_from — the only mechanism that shares the service's
anonymous and image volumes — and the hook's own volume declarations,
materialized by the standard path, take precedence per target. This is
what lets an init container get read-write access to a volume the
service mounts read-only (fixes: see PR).

e2e scenarios lock extra_hosts inheritance and accumulation, volume
override and completion, and the unit tests pin the merge rules.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-09-21 16:35:20 +02:00

21 lines
793 B
YAML

services:
app:
image: alpine
init: true
command: sleep infinity
extra_hosts:
- "inherited-host:192.0.2.10"
- "shared-host:192.0.2.11"
pre_start:
# no extra_hosts declared: the service's entries are inherited
- command: sh -c 'grep inherited-host /etc/hosts'
# declared extra_hosts complete the inherited ones; entries for the
# same hostname accumulate, exactly like the compose file merge rules
- command: >
sh -c 'grep declared-host /etc/hosts &&
grep inherited-host /etc/hosts &&
grep "192.0.2.99.*shared-host" /etc/hosts &&
grep "192.0.2.11.*shared-host" /etc/hosts'
extra_hosts:
- "declared-host:192.0.2.20"
- "shared-host:192.0.2.99"