Skip to content

fix(hooks): stop or restart a container whose pre_stop hook cannot run - #14547

Merged
ndeloof merged 1 commit into
docker:mainfrom
htoyoda18:fix/pre-stop-on-stopped-container
Nov 24, 2026
Merged

ndeloof merged 1 commit into
docker:mainfrom
htoyoda18:fix/pre-stop-on-stopped-container

Conversation

@htoyoda18

@htoyoda18 htoyoda18 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Compose runs pre_stop hooks as execs in the container, and a stopped or paused container rejects the exec with a conflict. Because of that, restart failed on a stopped or crashed service that has pre_stop hooks and left it stopped. stop on such a service while it was paused exited 0 but left the container paused.

With this change, a conflict skips the hooks, since there is no process to run them in, and the stop or restart goes on.

Related issue

Fixes #14545

Changes made

  • pkg/compose/hook.go: add runPreStopHooks, shared by stop and restart. It skips relay containers, as both call sites did before.
    • A not-found error reports the container as gone.
    • A conflict skips the remaining hooks and lets the caller go on.
    • Any other hook error is returned, as before.
  • pkg/compose/down.go: stopContainer uses the helper. A conflict no longer returns before ContainerStop, so a paused container is stopped. A container that is already gone is still not stopped, as before.
  • pkg/compose/restart.go: restartContainer uses the helper, so a conflict no longer aborts the restart.

Testing done

  • Unit tests pass: make test
  • E2E tests pass: make e2e-compose or make e2e-compose-standalone
  • Linting passes: make lint
  • Code formatted: make fmt
  • Manually tested (describe):

AI tool used (if applicable)

AI Tool:

Additional context

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

image

Signed-off-by: Hiroto Toyoda <hirototoyoda20020927@gmail.com>
@htoyoda18
htoyoda18 marked this pull request as ready for review November 24, 2026 13:48
@htoyoda18
htoyoda18 requested review from a team as code owners November 24, 2026 13:48
@htoyoda18
htoyoda18 requested review from glours and ndeloof November 24, 2026 13:48
@ndeloof
ndeloof requested a balanced review from Copilot November 24, 2026 14:08

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.

🟢 Approval recommended

No active review issues remain for the pre-stop hook handling changes.

No open issues 🎉

What changed in this PR

Fixes #14545 by allowing Compose to continue stopping or restarting a container when its pre_stop hooks cannot run because the container is stopped or paused.

Changes:

  • Centralizes pre-stop hook handling, skipping conflicts while preserving relay-container exclusions and other error handling.
  • Updates stop and restart to use the shared helper.
  • Adds unit and end-to-end regression coverage for stopped, paused, and removed containers.
File Description
pkg/​compose/​restart.go Uses the shared helper so hook conflicts no longer abort restart.
pkg/​compose/​restart_test.go Tests restarting a stopped container after a hook conflict.
pkg/​compose/​hook.go Adds shared pre-stop hook handling for conflicts and missing containers.
pkg/​compose/​down.go Continues stopping after hook conflicts while skipping removed containers.
pkg/​compose/​down_test.go Tests hook conflicts during stop and handling of removed containers.
internal/​e2e/​testdata/​TestPreStopHookStopPaused/​compose.yaml Adds a hooked service fixture for paused-container coverage.
internal/​e2e/​testdata/​TestPreStopHookRestartAfterStop/​compose.yaml Adds a hooked service fixture for restart-after-stop coverage.
internal/​e2e/​hooks_test.go Adds scenarios verifying restart after stop and stopping paused containers.

Review effort: Balanced

@ndeloof
ndeloof enabled auto-merge (rebase) November 24, 2026 14:14
@ndeloof
ndeloof merged commit 26b1a8f into docker:main Nov 24, 2026
63 of 67 checks passed
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/compose/restart.go 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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.

[BUG] pre_stop hooks make restart fail on a stopped service, and stop silently skip a paused one

3 participants