Skip to content

Pre start init containers#13862

Draft
glours wants to merge 2 commits into
docker:mainfrom
glours:pre-start-init-containers
Draft

Pre start init containers#13862
glours wants to merge 2 commits into
docker:mainfrom
glours:pre-start-init-containers

Conversation

@glours

@glours glours commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

What I did
Add support for the pre_start service lifecycle hook from the compose spec.

Hooks run as ephemeral containers before a service's containers start, sharing the first replica's volumes via VolumesFrom and attached to the same networks. A non-zero hook exit gates service start.

Semantics:

  • Runs once per service, only when no replica is already running (initial up, --force-recreate, spec change).
  • Not re-triggered on scale up.
  • per_replica: true is rejected up front; only per_replica: false is supported for now.
  • Hook image defaults to the service image (including build:); overridable per hook.

Covered by 8 unit tests and 10 E2E tests (success, failure gating, build inheritance, idempotent re-up, spec change, force-recreate, mid-sequence failure, ordering, scale-up, scaled service).

Note: the first commit adds a temporary replace to a compose-go fork exposing PreStart on ServiceConfig. To be reverted once a proper compose-go release lands.

Related issue
compose-spec/compose-spec#11 & compose-spec/compose-spec#291

(not mandatory) A picture of a cute animal, if possible in relation to what you did
image

glours added 2 commits June 22, 2026 10:47
Temporary replace directive pointing to a compose-go fork that exposes
PreStart hooks on types.ServiceConfig. To be removed once a compose-go
release with these fields is published.

Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
runPreStart executes a service's pre_start hooks sequentially as
ephemeral containers sharing the first service replica's volumes via
VolumesFrom and attached to the same networks. A non-zero hook exit
gates service start.

per_replica: false is the only currently supported mode; per_replica:
true is rejected up front. The log stream is opened before
ContainerStart to avoid racing AutoRemove on fast-exiting hooks, and
ContainerWait uses WaitConditionNextExit to block until the run
actually finishes.

pre_start runs once per service when no replica is already running
(initial up, force-recreate or spec change), and is skipped on
scale-up so additional replicas don't re-trigger the hooks.

E2E coverage: success path, hook failure gating start, build-image
inheritance, idempotent re-up, re-run on spec change, force-recreate,
mid-sequence failure, sequential ordering, no re-run on scale-up, and
once-per-service for scaled services.

Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 62.85714% with 52 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/compose/pre_start.go 60.34% 40 Missing and 6 partials ⚠️
pkg/compose/convergence.go 72.72% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@glours glours force-pushed the pre-start-init-containers branch from ff47ea5 to 8e44596 Compare June 22, 2026 09:25
@glours

glours commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

/review

@docker-agent

docker-agent Bot commented Jun 22, 2026

Copy link
Copy Markdown

PR Review Failed — The review agent encountered an error and could not complete the review. View logs.

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.

1 participant