mirror of
https://github.com/docker/compose.git
synced 2026-11-21 23:50:17 +00:00
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>
17 lines
419 B
YAML
17 lines
419 B
YAML
services:
|
|
app:
|
|
image: alpine
|
|
init: true
|
|
command: sleep infinity
|
|
volumes:
|
|
- data:/data:ro
|
|
- config:/config:ro
|
|
pre_start:
|
|
# declares only /data read-write: /config must still be visible,
|
|
# inherited through volumes_from
|
|
- command: sh -c 'test -d /config && echo "saw-config" > /data/init.txt'
|
|
volumes:
|
|
- data:/data:rw
|
|
volumes:
|
|
data: {}
|
|
config: {}
|