Looking for an easy way to periodically monitor the status of your Docker containers? Want to feel safe knowing its status via email? Well, I got a solution for you that we recently used for one of our clients.

The idea here is to check the container status at a regular interval and send an email alert based on its state. This requires three steps and they are:
- Configure Container Name
- Configure SendGrid keys
- Run a periodic check using cronjob.
Configure Container Name:
CONTAINER_NAME
You may ask why are we trying to get the container name instead of container id? The answer is, container id will change when we restart the service while the container name will persist.
Here is the shell script to monitor the container :
#!/bin/bash #Description :if container is down it will trigger the mail and logs #Author :tejaswini
#variables
#sendgrid api key ,user, password
SENDGRID_API_KEY=""
SENDGRID_USER=""
SENDGRID_PASSWORD=""
SUBJECT="service down alert for "
#sending mail as
FROM_EMAIL=""
#sendig user
FROM_USER="$user"
#sending mail to
EMAIL_TO=""
#container name
CONTAINER_NAME=""
servicevalue=$(docker inspect -f {{.State.Running}} "$CONTAINER_NAME")
if [ "$servicevalue" = true ]; then
echo "$service is running!!!"
else
mailfile=$(echo "Please check your service $service on ${HOSTNAME}" )
bodyHTML="<p> $mailfile </p>"
maildata='{"personalizations": [{"to": [{"email": "'${EMAIL_TO}'"}]}],"from": {"email": "'${FROM_EMAIL}'", "name": "'${FROM_NAME}'"},"subject": "'${SUBJECT}'","content": [{"type": "text/html", "value": "'${bodyHTML}'"}]}'
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer '$SENDGRID_API_KEY \
--header 'Content-Type: application/json' \
--data "'$maildata'"
fiConfigure SendGrid Keys:
In the script above, the below-listed variables or params helps you to configure the SendGrid Keys
SENDGRID_API_KEY
SENDGRID_USER
SENDGRID_PASSWORD
FROM_EMAIL
EMAIL_TO
Run a periodic check using cronjob.
Open the crontab editor using crontab -e and append the following line to it.
10 * * * * /bin/bash path/containermoniter.sh
That’s it. You got the script that can monitor your container services.
Wait! Wait! Wait! Don’t go. I have something more for you. As you know, when we deploy the application in Swarm mode, the container name will change and so we need a nice way to change the variable value “CONTAINER_NAME”.
You can easily do it by overriding the following configuration in the script. They are:
- Stack name
- Service name
- Change the if condition
Here is the updated script for you:
#!/bin/bash #Description :if container is down it will trigger the mail and logs
#variables
#sendgrid api key ,user, password
SENDGRID_API_KEY=""
SENDGRID_USER=""
SENDGRID_PASSWORD=""
SUBJECT="service down Alert of "
#sending mail as
FROM_EMAIL=""
#sendig user
FROM_USER="$user"
#sending mail to
EMAIL_TO=""
#container name
CONTAINER_NAME=""
#stack name
stackname=””
#service name
servicename=""
container=$(docker stack ps “$stackname” | awk ‘{print $2}’| grep “$servicename” | wc -l)
if [ “$container” -ge 1 ]; then
echo "$service is running!!!"
else
echo ""$service" is not runing"
mailfile=$(echo "Please check your service $service on ${HOSTNAME}" )
bodyHTML="<p> $mailfile </p>"
maildata='{"personalizations": [{"to": [{"email": "'${EMAIL_TO}'"}]}],"from": {"email": "'${FROM_EMAIL}'", "name": "'${FROM_NAME}'"},"subject": "'${SUBJECT}'","content": [{"type": "text/html", "value": "'${bodyHTML}'"}]}'
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer '$SENDGRID_API_KEY \
--header 'Content-Type: application/json' \
--data "'$maildata'"
fiI hope you will find the above script(s) useful. I look forward to hearing your comments and suggestions as this is my first blog at Spritle.

HI Tejaswini,
Thanks for sharing. Good one. All the very best for your bright future.
Hi Tejaswini,
Thanks, The script works fine for a single container, how to monitor multiple containers with the use of this script?
Hi Tejaswini,
I have a question.suppose the user is working on a rstudio container.Now I want to write a script in such a way that it will monitor user action whether he is active or inactive.if the user is inactive for a particular time it will terminate the pod on which it is running
Hi ,
I would really appreciate for your post, but as a Devops engineer I too have the same requirement of monitor docker container using the script but the problem is the containers are hosted in Private IP. Please let me know if you can help me in resolving the issue.