There are no such tools till now to monitor whether your Docker container is running or not. Now, I had a Docker container running in production and I could not afford not knowing whenever it went down or rather stopped. So, I decided to do something about it. The blog is about the same.
What we will be doing is checking whether the required Docker container is up using a simple bash script. We will put this script in cron running at every minute. Every minute if the container is up, the count remains 1 for single container in my use-case and is pushed to AWS CloudWatch. In AWS CloudWatch we will put an alarm for count <1 and an SNS notification to follow.
- AWS Linux server
- Docker service and a Docker container running on that server
- The server should have AWS CLI installed and have appropriate IAM keys or a role attached to it. The policy attached to the role should have CloudWatch access.
1. Understand the bash script
You need a bash script which will check whether your Docker container is up and running or not. Below is the bash script you can use. If you have multiple Docker containers you simply need to grep that as well and change the count. Else you can simply create a different metric for different container.
docker ps | grep -i "$container"
if [ "$?" -ne 0 ];then
/usr/local/bin/aws cloudwatch put-metric-data –metric-name "Docker Container $container is down on `hostname`" –unit Percent –value "$count" –dimensions InstanceId=$INST_ID –namespace System/Linux
/usr/local/bin/aws cloudwatch put-metric-alarm –alarm-name Docker-Prod-Container-Down –alarm-description "If the named container is down this alarm is triggered" –metric-name Docker Container is down on `hostname` –namespace System/Linux –statistic Average –period 60 –threshold $count –comparison-operator LessThanThreshold –dimensions Name=InstanceId,Value=$INST_ID –evaluation-periods 1 –alarm-actions arn:aws:sns:us-east-1:069016302557:ProdContainer –unit Count
Explanation of the above script
- container variable stores the container id which you want to monitor.
- docker ps will list all the running instances and grep will check if container with that name is present or not.
- Now, we check if grep found the container name or not. If no then count will be 0, else will be 1. count == 0 will mean container is not running and count == 1 will mean that container is running.
- Next, we create a CloudWatch metric pushing the value of count to AWS CloudWatch metric in the namespace System/Linux.
- Last is creating an alarm for the metric. Once the metric data starts getting pushed you can put an alarm either via AWS console or through the command in the alarm1 function in the script.
2. To execute the script first time just run the following command:
[js]sudo bash dockermonitor.sh metric1
sudo bash dockermonitor.sh alarm1[/js]
3. Put the script in Cron.
Now, just put the first command in Cron using crontab -e and appending the command there as shown below. You don’t need to run the alarm1 function again.
* * * * * bash /home/dockermonitor.sh metric1
This script will do all for you. It will push the data to CloudWatch and monitor your Docker Container. It will also trigger any alarms when your Docker container is down. This might also be helpful in situations where you might have just restarted your container and it may have never come up and you getting to know about it after sometime.