Run a script when docker is stopped
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running cleanup logic when Docker stops a container is a common operational requirement for flushing logs, releasing locks, or publishing final metrics. The safest place for this logic is usually inside the container process itself, where stop signals can be handled directly. External host hooks can work too, but they add coupling and deployment complexity.
Handling Stop Signals in the Container
When Docker stops a container, it sends SIGTERM to the main process, waits for a grace period, then sends SIGKILL if needed. If your process traps SIGTERM, you can run a script before exit.
Use this script as container entrypoint so it receives stop signals directly. If you run a child process tree, ensure signals are forwarded correctly.
Dockerfile and Runtime Wiring
A common production setup is an entrypoint script that launches the app and traps termination events. Keep the script short and deterministic.
Run and stop to verify behavior:
If the cleanup action needs external persistence, write to a mounted volume or send data over the network before exit.
Host Side Alternatives with Docker Events
Sometimes you cannot modify the container image. In that case, listen for Docker stop events on the host and trigger a script externally.
This pattern is useful for platform level automation, but it is less portable than in container signal handling and usually needs stronger host permissions.
Coordinating Shutdown in Orchestrated Environments
In Kubernetes or other orchestrators, container stop hooks interact with higher level lifecycle rules. For example, a pod termination grace period controls how long your cleanup can run before force termination. Align script runtime with that budget.
You can also use platform lifecycle hooks for additional control, but process signal handling should remain the primary mechanism because it is closest to the app.
Test real shutdown timing under load, not only idle conditions. A script that finishes in two seconds locally may take much longer when network storage or remote APIs are involved. Make the cleanup step idempotent so repeated stop signals or retries do not create duplicate side effects. Idempotency turns many operational edge cases into safe no op behavior.
Record cleanup start and end timestamps in logs so operators can verify shutdown behavior quickly during incidents. Clear shutdown telemetry reduces guesswork when containers terminate unexpectedly.
Common Pitfalls
- Running cleanup in a process that never receives
SIGTERM. - Using shell wrappers that do not forward signals to child processes.
- Assuming cleanup always finishes, even when stop timeout is too short.
- Writing final data to ephemeral paths that disappear after container exit.
- Depending on host event listeners without monitoring their health.
Summary
- Prefer in container signal traps for stop time scripting.
- Keep entrypoint logic minimal and predictable.
- Test shutdown under realistic timeout settings.
- Use host level
docker eventsonly when image changes are impossible. - Persist critical shutdown output to durable storage.

