← Back to all posts

Does Docker Compose Dry Run Execute post_start Hooks?

Compare a Compose dry run with a disposable startup, and verify the post_start result that your pipeline actually depends on.

An open blue shipping container holding drawers and a small database

A Compose dry run can show a container starting while a command in post_start remains untested. If the hook registers the service or writes a file other work depends on, a successful plan gives you only part of the evidence.

Docker Compose v5.7.2, released November 17, 2026, changed the dry-run path to skip lifecycle hooks instead of failing during exec attachment. That makes the dry run usable for planning with a hook configured. The hook itself still needs a real container and an observable result. Compose v5.7.2 release notes

Give the hook one observable result

Save the following as compose.yaml in a fresh directory. It uses an Alpine container with no ports or host mounts. The hook writes a marker inside the container.

services:
  marker:
    image: alpine:3.22
    command: ["sleep", "600"]
    post_start:
      - command: ["sh", "-c", "printf 'hook ran\\n' > /tmp/compose-hook-marker"]

Use a working Docker daemon and a Compose version with the dry-run fix. Choose an unused Compose project name so the test and cleanup are scoped to the fixture.

docker compose version
docker compose -p hook-smoke config --quiet
docker compose -p hook-smoke --dry-run up -d
docker compose -p hook-smoke ps --all

config --quiet validates and resolves the Compose file. The dry run previews Compose’s actions. In a fresh project, ps --all should show no container from the dry run. This step checks the planner, even if the image is already present locally.

Start the disposable project and check the file

docker compose -p hook-smoke up -d
docker compose -p hook-smoke exec -T marker sh -c '
  test "$(cat /tmp/compose-hook-marker)" = "hook ran"
'

The final command checks the contents and returns a failing status if the marker is missing or wrong. In CI, capture the check’s exit status and run teardown in an always-run cleanup step.

docker compose -p hook-smoke down

The real startup may pull the Alpine image if it is missing. Use a pinned digest instead of a tag if the fixture needs reproducible image contents across CI runs. Docker also documents that post_start has no guaranteed ordering relative to the container’s entrypoint. A service that must finish initialization before accepting requests should make that prerequisite explicit through its startup or readiness design. Compose lifecycle hooks

Replace the marker in a real smoke test with the observable effect your service needs. For a registration hook, query the registration from the test environment. For a generated file, check its contents and ownership. A container listed as running provides a different signal from either check. For practice with service definitions before adding lifecycle hooks, use the LabEx Compose build exercise.

References