Monitoring Docker container status using AWS CloudWatch

29 / Sep / 2015 by Ranvijay Jamwal 3 comments

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.

cicd-with-docker-on-aws-1-638
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.

Prerequisites

  1. AWS Linux server
  2. Docker service and a Docker container running on that server
  3. 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.

Steps

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.

[js]

#!/bin/bash

INST_ID=i-9dee2454
container=asa-prod
$
metric1()
{

docker ps | grep -i "$container"

if [ "$?" -ne 0 ];then
count=0
else
count=1
fi
/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
}

alarm1()
{
/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
}
$1
[/js]

 

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.

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. vilas

    Hi,

    I have followed above process and created the above script and ran on my aws instance. But how do I get to see the metrics for that in cloudwatch. I dont see anything happening in cloudwatch after running that script

    Reply
    1. Chandrapratap Singh

      I am facing problem while running the script, giving error

      dockermonitor.sh: line 4: $: command not found

      Reply
  2. Jeffrey

    Hello Harish,First I must thank you for your time putting up a very useufl article. I found this page after visiting “Architecting an Highly Available and Scalable WordPress Site in AWS ” page. I’m currently running a micro instance and I’ve installed a wordpress blog there. I’m using above explained simple structure.I have few question.

    1. My EC2 EBS volume keeps growing without me installing anything there. When I finished the setup of wordpress there, it was somewhere around 5GB and now it’s somewhere around 7GB. I haven’t posted any blog posts. Why is it keep increasing in size?

    2. On average, is above simple setup suitable for if I receive 6000-15000 visitors per day.Thank you once again for your excellent collection of articles on Amazon EC2 WordPress Setup.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *