Skip to content

Add pre_start lifecycle hook for init containers#647

Merged
ndeloof merged 1 commit into
compose-spec:mainfrom
glours:init-containers-pre-start-proposal
Jun 16, 2026
Merged

Add pre_start lifecycle hook for init containers#647
ndeloof merged 1 commit into
compose-spec:mainfrom
glours:init-containers-pre-start-proposal

Conversation

@glours

@glours glours commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:
Introduce pre_start as a sequence of init containers run before a service starts.
Each step runs to completion in declared order, in its own ephemeral container created after ContainerCreate but before ContainerStart, with the service container's mounts shared. A non-zero exit fails the bring-up of the service and its dependents.

This is the first-class replacement for the restart: "no" plus service_completed_successfully pattern.

Which issue(s) this PR fixes:

Follow up of #11

Introduce pre_start as a sequence of init containers run before a
service starts. Each step runs to completion in declared order, in
its own ephemeral container created after ContainerCreate but before
ContainerStart, with the service container's mounts shared. A non-zero
exit fails the bring-up of the service and its dependents.

This is the first-class replacement for the restart: "no" plus
service_completed_successfully pattern.

Signed-off-by: Guillaume Lours <[email protected]>
Signed-off-by: Guillaume Lours <[email protected]>
@glours
glours force-pushed the init-containers-pre-start-proposal branch from 2b4e69f to 2c4838d Compare June 16, 2026 08:49
@ndeloof
ndeloof merged commit a076fad into compose-spec:main Jun 16, 2026
3 checks passed
glours added a commit to glours/compose-go that referenced this pull request Jun 16, 2026
Implements the `pre_start` service field introduced in
compose-spec/compose-spec#647: an ordered list
of init containers run to completion before the service container
starts. Each step runs in its own ephemeral container; a non-zero exit
fails the bring-up of the service and its dependents.

Compose-go owns parsing, validation via JSON schema, and image
inheritance: when a pre_start hook omits `image`, it is normalized
to the parent service's image. Volume/network inheritance and the
runtime execution stay with docker/compose.

Signed-off-by: Guillaume Lours <[email protected]>
glours added a commit to glours/compose-go that referenced this pull request Jun 29, 2026
Implements the `pre_start` service field introduced in
compose-spec/compose-spec#647: an ordered list
of init containers run to completion before the service container
starts. Each step runs in its own ephemeral container; a non-zero exit
fails the bring-up of the service and its dependents.

Compose-go owns parsing, validation via JSON schema, and image
inheritance: when a pre_start hook omits `image`, it is normalized
to the parent service's image. Volume/network inheritance and the
runtime execution stay with docker/compose.

Signed-off-by: Guillaume Lours <[email protected]>
glours added a commit to compose-spec/compose-go that referenced this pull request Jun 29, 2026
Implements the `pre_start` service field introduced in
compose-spec/compose-spec#647: an ordered list
of init containers run to completion before the service container
starts. Each step runs in its own ephemeral container; a non-zero exit
fails the bring-up of the service and its dependents.

Compose-go owns parsing, validation via JSON schema, and image
inheritance: when a pre_start hook omits `image`, it is normalized
to the parent service's image. Volume/network inheritance and the
runtime execution stay with docker/compose.

Signed-off-by: Guillaume Lours <[email protected]>
@psviderski

Copy link
Copy Markdown

It's really great to see the progress on init containers in docker compose and the spec. Awesome work! 👍

I designed something very similar as a x-pre_deploy compose extension in Uncloud: https://uncloud.run/docs/guides/deployments/pre-deploy-hooks/
Will look into supporting/replacing it with pre_start. Is there any public discussion happening on this design and changes?

aevesdocker added a commit to docker/docs that referenced this pull request Jul 2, 2026
<!--Delete sections as needed -->

## Description

As per docker/compose#13862 and
compose-spec/compose-spec#647

## Related issues or tickets

<!-- Related issues, pull requests, or Jira tickets -->

## Reviews

<!-- Notes for reviewers here -->
<!-- List applicable reviews (optionally @tag reviewers) -->

- [ ] Technical review
- [ ] Editorial review
- [ ] Product review

---------

Co-authored-by: Guillaume Lours <[email protected]>
glours added a commit that referenced this pull request Jul 17, 2026
… triggers

Follow-up to #647, addressing gaps identified while implementing the hook:

- state explicitly that pre_start steps only run once the service's
  depends_on conditions have been satisfied
- define per_replica: true semantics: one run per replica, tied to that
  replica's mounts and start; declared order preserved per replica, runs
  for distinct replicas may execute concurrently; a failed run prevents
  that replica from starting and fails the service bring-up
- state that per_replica: false on a scaled service is not an error when
  per-instance mounts exist; such steps simply run without access to them
- define "definition changes" (any attribute) and "service is recreated"
  (container recreation: config change, forced recreate) as re-run triggers
- replace the tmpfs example on per_replica: volume sharing relies on
  volumes_from, which does not propagate tmpfs mounts
- fix broken post_start link in the pre_stop section

Signed-off-by: Guillaume Lours <[email protected]>
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.

3 participants