Unable to run compose service pre_stop hook

I am unable to run my service pre_stop hook. The YAML

services:
  hooks-test:
    image: alpine
    command: /bin/sh -c "echo this is command; sleep 3600”
    post_start:
    - command: /bin/sh -c "echo this is post_start”
    pre_stop:
    - command: /bin/sh -c "echo this is pre_stop”

does not produce the pre_stop output, when stopped by any of: Ctrl+C, compose down, or compose stop.

Steps to reproduce:

$ docker compose -f hooks.yaml  up
[+] Running 1/1
 ✔ Container container-hooks-test-1  Created                                                                              0.0s
Attaching to hooks-test-1
hooks-test-1  | this is command
hooks-test-1 ->  | this is post_start
(Ctrl+C)
Gracefully stopping... (press Ctrl+C again to force)
[+] Stopping 1/1
 ✔ Container container-hooks-test-1  Stopped                                                                             10.3s

My config:

  • OS: MacOs Sonoma 14.7.1
  • Docker Compose version v2.33.1-desktop.1

How do I get the pre_stop hook to run?

I don’t know if that hook (or any hook) was intended to show outputs. The documentation shows chown as example. If you think it is a bug, you could try searching for issue son GitHub and eport it if you don’t find one

If you just want to see the that the hook was executed, you can redirect the output to a file instead.

services:
  hooks-test:
    image: alpine
    command:
      - sleep
      - inf
    init: true
    post_start:
      - command: /bin/sh -c "echo this is post_start"
    pre_stop:
      - command:
          - /bin/sh
          - -c
          - |
            echo "pre_stop hoook" > /app/out.txt
    volumes:
      - .:/app