Skip to content

fix(plan): run the pre_stop hooks before every stop the plan makes - #14555

Merged
glours merged 2 commits into
docker:mainfrom
ndeloof:plan-pre-stop
Nov 27, 2026
Merged

glours merged 2 commits into
docker:mainfrom
ndeloof:plan-pre-stop

Conversation

@ndeloof

@ndeloof ndeloof commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

The pre_stop hooks become an operation of the reconciliation plan, so that every container up stops runs them, the one a recreation replaces included, and a recreation runs them before it creates the replacement.

Related issue

First sub-task of #14551.

Context

#14477 made up run the pre_stop hooks when it scales a service down or stops a container for a recreated network, volume or dependency. It does so inside the executor's stop, and skips them for the container a recreation replaces: the hooks would run after its replacement is created under a temporary name, and one that fails would leave that replacement behind for the next up to trip on. So the same hook still runs or not depending on why up stops the container, and stop, restart and down each carry their own copy of the logic.

What the PR brings

  • The hooks are an operation of the plan (RunPreStop), and the stop depends on it. Every stop the plan makes has it: a recreated container, a scale down, the containers of a recreated network or volume, the dependents restarted with a recreated dependency, a service that moves to a provider. The executor's stop is a plain stop again, and SkipPreStop is gone.
  • A recreation runs the hooks first, then creates the replacement. One that fails then leaves nothing behind, and the container a recreation replaces no longer goes without its hooks. The old container stays up from the end of its hooks to its stop, the creation of the replacement included.
  • Same behavior as restart and down. A container with no process to run the hooks in (stopped, paused) skips them and is stopped all the same; a relay has no hooks to run; a dry run runs none. A hook that fails fails up, and the container it was about to stop is left running.
  • Once per container. A container stopped for two reasons in one plan (a recreated network and a changed service) is stopped once, and its hooks run once.
  • The row shows it. The row of a container is "stopping" while its hooks run, and a recreation is still one Recreate / Recreated row.
  • Nothing changes without hooks. The plans of services with no pre_stop hook are the ones they were, golden tests included. Orphans are left out: their service is not in the model, and nothing the engine stores says what their hooks were.

This changes what users with pre_stop hooks see: the hook now also runs when up recreates a container, which is what the hook is for. As for restart and down, the hooks are the ones of the model being applied, which a change of hooks makes differ from the ones the old container was created with.

Why this is the right first brick

stop, restart and down are still to move onto the plan (#14551). With the hooks a node of the plan, they stop being something only those three know how to do, and the next steps can reuse it instead of carrying a copy.

Testing done

AI tool used

Claude Code (Anthropic) wrote the change, with review by the submitter; independently reviewed with the GitHub Copilot CLI.

Additional context

The tests are the bulk of the change: reconcile_pre_stop_test.go (plan), executor_pre_stop_test.go (execution) and hooks_plan_stop_test.go (e2e, with RunningContainersKept added to checks.go). Rebasing on #14477 removed its two unit tests of the executor-side hooks, superseded by these.

@ndeloof
ndeloof requested review from a team as code owners November 25, 2026 09:25
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Container stops are not deduplicated by ID, allowing scale-down or provider migration to schedule duplicate hook/stop chains that can run hooks concurrently or interrupt them.

1 open issue
What changed in this PR

This PR adds pre_stop hooks to Compose’s reconciliation plan, bringing plan-driven stops closer to stop, restart, and down behavior. It is the first step toward the lifecycle consolidation tracked in #14551.

Changes:

  • Adds a RunPreStop operation before plan-driven stops and replacement creation.
  • Integrates hook execution and progress reporting.
  • Adds unit and end-to-end coverage for ordering, failures, skipped hooks, and recreation recovery.
File Description
pkg/​compose/​reconcile.go Schedules hooks before stops and replacement creation.
pkg/​compose/​reconcile_pre_stop_test.go Tests hook planning, ordering, and reuse.
pkg/​compose/​executor.go Dispatches the new hook operation.
pkg/​compose/​executor_pre_stop_test.go Tests execution, failures, skips, and progress events.
pkg/​compose/​executor_ops.go Executes hooks through the existing hook runner.
pkg/​compose/​executor_events.go Reports stopping progress while hooks run.
internal/​planning/​plan.go Defines the RunPreStop operation.
internal/​planning/​plan_test.go Tests the operation’s display name.
internal/​e2e/​testdata/​TestUpSkipsPreStopHookOfAStoppedContainerItRecreates/​compose.yaml Provides the stopped-container fixture.
internal/​e2e/​testdata/​TestUpRunsPreStopHookOfTheReplicaItScalesDown/​compose.yaml Provides the scale-down fixture.
internal/​e2e/​testdata/​TestUpRunsPreStopHookOfTheContainerItRecreates/​compose.yaml Provides the recreation fixture.
internal/​e2e/​testdata/​TestUpKeepsTheContainerWhoseFailingPreStopHookBlocksItsReplacement/​compose.yaml Provides configurable hook failure and recovery.
internal/​e2e/​hooks_plan_stop_test.go Adds four hook lifecycle scenarios.
internal/​e2e/​checks.go Adds a check that running containers remain running.

Review effort: Balanced

Comment thread pkg/compose/reconcile.go Resolved

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

The stop of the reconciliation plan runs the pre_stop hooks itself, in the
executor, since docker#14477, except the stop of a recreate: the
replacement is created under a temporary name before the old container is
stopped, and a failing hook would leave it behind for the next up to trip on.
So a container that `up` replaces still goes without its hooks.

The hooks are now an operation of the plan, RunPreStop, that the stop depends
on. Every stop the plan makes has it: a recreated container, a scale down, the
containers of a recreated network or volume, the dependents restarted with a
recreated dependency, a service that moves to a provider. A container that has
no process to run them in skips them and is stopped all the same, as for
restart and down; a hook that fails fails the plan and leaves the container
running. A container stopped for two reasons is stopped once, and its hooks
run once.

A recreation runs the hooks before it creates the replacement: one that fails
then leaves nothing behind, and no stop has to skip them any more.

The plans of services without pre_stop hooks are unchanged.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
A scale down or a move to a provider added its own stop (and its own
pre_stop hooks) for a container an earlier stage, a recreated network, had
already stopped: the hooks ran twice, concurrently, and one stop could
interrupt the hooks of the other. planStop now reuses the stop already
scheduled for the container and records the one it adds.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

@glours glours left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@glours
glours merged commit 60046ef into docker:main Nov 27, 2026
62 checks passed
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.

4 participants