background-hooknotifierbackground-hooknotifier
Last update: Jul 05, 20261 minutes read

Get notified when a Docker container stops

low-code

Get a push notification the moment a Docker container stops or restarts. A short script watches Docker events and calls your Hook.Notifier URL. Free.

To get notified when a Docker container stops, run a small script that listens to Docker's event stream and calls your Hook.Notifier URL when a container dies. The alert lands on your phone with the container name, right away.

Docker keeps most things running, but when a container exits unexpectedly and the restart policy papers over it, the failure can hide for hours. Watching the event stream surfaces it immediately.

Watch the Docker event stream

Docker emits an event every time a container starts, stops, or dies. This script pings you on the ones that matter:

#!/bin/bash
# /usr/local/bin/watch-docker.sh
docker events --filter 'event=die' --format '{{.Actor.Attributes.name}}' | while read name; do
  curl -s "https://hooknotifier.com/{IDENTIFIER}/{KEY}?object=Container%20died&body=${name}%20stopped%20unexpectedly&priority=high&color=%23EE6767&tags=docker"
done

Run it as a background service (a small systemd unit or nohup) so it keeps listening.

hook notifier notification

First get your URL

Your Hook.Notifier URL is https://hooknotifier.com/{IDENTIFIER}/{KEY}. Create a free account to get yours, then drop it into the script.

Watch only the containers you care about

Most stacks have chatty containers you do not need alerts for. Filter by name or label so you only hear about the important ones:

docker events --filter 'event=die' --filter 'label=notify=true' --format '{{.Actor.Attributes.name}}'

Then add notify=true as a label to the containers that matter in your compose file:

services:
  api:
    labels:
      - "notify=true"

Keep it running with systemd

# /etc/systemd/system/watch-docker.service
[Unit]
Description=Notify on Docker container death
After=docker.service

[Service]
ExecStart=/usr/local/bin/watch-docker.sh
Restart=always

[Install]
WantedBy=multi-user.target

Enable it with systemctl enable --now watch-docker. Now a dying container always reaches your phone.

Why it matters

Restart policies are a double-edged sword: they keep a service alive, but they also hide the fact that it keeps falling over. A push on every unexpected death means you notice the pattern before it becomes an outage.

New to this? Start with how to send yourself a native push notification, or see how to get notified when a server runs low on disk.