A simple but powerful bash script that monitors your Docker containers and sends Slack notifications when containers crash. Perfect for keeping tabs on your containers' health without constantly checking logs.
Ever had a Docker container silently die on you? Yeah, me too. That's why I built this. It sits quietly in the background watching your containers, and the moment one crashes - BAM! - you get a Slack notification with all the important details:
- Container name
- Container ID
- Image used
- Exit code
- How long it ran
- Which host it was running on
First, let's get the monitoring script and service file where they need to be:
Create the monitor script:
# Create the script file
sudo nano /usr/local/bin/docker-monitor.sh
# Make it executable
sudo chmod +x /usr/local/bin/docker-monitor.shCreate the service file:
sudo nano /etc/systemd/system/docker-monitor.serviceCopy the contents from both files in this repo into these new files.
- Go to your Slack workspace
- Create a new webhook (or use an existing one) at https://api.slack.com/apps
- Copy the webhook URL
- Replace the
SLACK_WEBHOOK_URLin the script with your URL:sudo nano /usr/local/bin/docker-monitor.sh # Find and replace this line: SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
Time to get this baby running:
# Reload systemd to pick up the new service
sudo systemctl daemon-reload
# Enable it to start on boot (because who wants to do this manually?)
sudo systemctl enable docker-monitor
# Start it right now
sudo systemctl start docker-monitorLet's make sure everything's running smoothly:
# Check the service status
sudo systemctl status docker-monitor
# Take a peek at the logs
sudo journalctl -u docker-monitor -f
# or
sudo tail -f /var/log/docker-monitor.logHere are some handy commands you might need:
# Stop the monitor
sudo systemctl stop docker-monitor
# Start it up again
sudo systemctl start docker-monitor
# Restart it (turn it off and on again, the universal fix!)
sudo systemctl restart docker-monitor
# Don't want it to start on boot?
sudo systemctl disable docker-monitor
# Check its current status
sudo systemctl status docker-monitor-
Check if the service is running:
sudo systemctl status docker-monitor
-
Make sure your Slack webhook is correct:
# Test the webhook directly curl -X POST -H 'Content-type: application/json' \ --data '{"text":"Test message"}' \ YOUR_WEBHOOK_URL
-
Check the logs:
sudo journalctl -u docker-monitor -f
This shouldn't happen with the latest version, but if it does:
- Stop the service
- Clear the logs
- Restart the service
sudo systemctl stop docker-monitor
sudo truncate -s 0 /var/log/docker-monitor.log
sudo systemctl start docker-monitorJust so you know where everything lives:
- Script:
/usr/local/bin/docker-monitor.sh - Service file:
/etc/systemd/system/docker-monitor.service - Log file:
/var/log/docker-monitor.log