← Back to blog

How to Monitor Docker Containers with Real-time Alerts

September 8, 2025 · Guides

It's 3 AM and your users are complaining that your app is down. You check your server only to discover a critical container crashed hours ago. Sound familiar?

This scenario plays out more often than you'd think and we've all been there. Running apps in Docker is convenient; containers start quickly and make it easy to run multiple services on the same server. But there's one problem that catches both beginners and experienced engineers: containers can stop or crash silently, and you might not notice until your users start complaining.

Sure, you could manually check your containers by running docker ps every few hours, but that's tedious and, let's be honest, completely unreliable. What you need is a system that watches your containers 24/7 and alerts you the moment something goes wrong.

That's where Docker's built-in events system comes to the rescue. With a simple bash script and a Discord webhook (feel free to swap it out for any webhook endpoint), you can get instant notifications whenever a container starts, stops, restarts, or dies. No heavy monitoring tools like Prometheus required.

This tutorial will guide you through setting up a lightweight monitoring system that gives you immediate visibility into your container health. By the end, you'll have real-time alerts running on your server, and you'll never again wake up to surprise downtime.

Prerequisites

  • Docker installed and running
  • Discord webhook URL ready
  • Basic command line familiarity

Introducing the Docker Events API

Docker provides an event stream that reports activity from the underlying Docker engine. This stream includes all container lifecycle changes such as:

  • Container created
  • Container started
  • Container stopped
  • Container died (crashed)
  • Container removed

You can access this stream directly from the command line. Try running:

docker events --filter type=container

This will print a continuous feed of events as they happen. If you start a container in another terminal, you'll see the corresponding container start event appear in the feed.

For example:

2025-09-08T14:32:01.123456789Z container start abc123 (nginx:latest, name=webapp)
2025-09-08T14:35:47.987654321Z container die abc123 (exitCode=1, name=webapp)

The output is raw, but it contains all the information you need. The key pieces are the timestamp, the action, and the container name.

Creating Your Docker Container Monitoring Script

So with the Discord webhook URL to hand, and the docker events command, lets put together a bash script that:

  • Listens for container events using docker events.
  • Extracts the timestamp, action, and container name.
  • Converts the timestamp to a human-readable format.
  • Sends the event as a message to Discord using curl.

Here is the script:

#!/bin/bash

# Replace with your Discord webhook URL
WEBHOOK_URL="https://discord.com/api/webhooks/XXXX/XXXX"

# Listen for container events and format output
docker events --filter type=container \
  --format '{{.Time}} {{.Status}} {{.Actor.Attributes.name}}' | while read timestamp status name
do
  # Convert epoch timestamp to human-readable format
  datetime=$(date -d @"$timestamp" +"%Y-%m-%d %H:%M:%S")

  # Create Discord message with container name and status
  message="🐳 [$datetime] $status *$name*"

  # Send message to Discord
  curl -H "Content-Type: application/json" \
       -X POST \
       -d "{\"content\": \"$message\"}" \
       "$WEBHOOK_URL"
done

Save the file as docker-discord-monitor.sh, and make it executable. Then we'll give it a quick test.

sudo mv docker-discord-monitor.sh /usr/local/bin/docker-discord-monitor.sh
sudo chmod +x /usr/local/bin/docker-discord-monitor.sh
./docker-discord-monitor.sh

# In another terminal, start/stop a container to see alerts

To run it as a background service, we'll create a systemd Service File:

nano /etc/systemd/system/docker-discord-monitor.service

In this file, we'll add the following:

[Unit]
Description=Docker Discord Monitor
After=docker.service
Requires=docker.service

[Service]
Type=simple
ExecStart=/usr/local/bin/docker-discord-monitor.sh
Restart=always
RestartSec=5
User=root
# Optionally, you can run as a non-root user if they have Docker access:
# User=youruser

[Install]
WantedBy=multi-user.target

Finally, we'll start the background process with systemctl

sudo systemctl daemon-reload
sudo systemctl enable docker-discord-monitor.service
sudo systemctl start docker-discord-monitor.service

Now your Docker to Discord monitoring script will run 24/7 without the need for you to run it in an active terminal session. You can check it's status any time with:

sudo systemctl status docker-discord-monitor.service

Testing Your Docker Alerts

Once the script is running, try starting and stopping containers to test that it works as a background service too. If successful you'll see messages appear in your Discord channel.

Limitations of the Script

This script is a great way to learn how the Docker Events API works, and it can be useful for small setups. But it does have some limitations:

  • Managing the script across multiple servers can become repetitive.
  • If the server goes offline, you won't be alerted
  • Basic reporting with no context on why the event occured

For hobby projects or early experiments, this is often good enough. But for production workloads, you will want a more structured solution.

Final Thoughts

Congratulations! You now have a robust Docker monitoring system that will alert you instantly when containers change state. Your setup includes:

  • Real-time container event monitoring using Docker's native API
  • Automated Discord notifications with timestamps and container names
  • A systemd service that runs 24/7 and restarts automatically
  • Zero external dependencies beyond Docker and curl

The beauty of this solution lies in its simplicity. You're leveraging Docker's native capabilities without adding complex external dependencies. Most importantly, you'll never again experience that sinking feeling of discovering a crashed container hours after the fact.

Your containers are now being watched 24/7. Sleep better knowing that if anything goes wrong, you'll be the first to know, not your users.

Beyond Single-Server Scripts

This script works perfectly for monitoring containers on a single server. But once you're managing multiple servers, each with multiple containers, filtering through floods of Discord notifications quickly becomes a full-time job.

Serversinc handles all of this automatically. We built container and server monitoring directly into the platform so you don't need to maintain scripts or service files yourself. Monitor your containers from one dashboard, get smart alerts that filter out noise, and access real-time logs without SSH-ing into machines.

Try Serversinc free for 14 days - no credit card required, no script maintenance needed.