How to Monitor and Alert Docker Container Status on EC2 for High Availability

In this blog, we will learn how to monitor the status of docker containers on EC2 and send email alerts if they stop running.

**Have you ever faced the problem of docker containers stopping unexpectedly?**

To save server costs, we use one EC2 instance to run multiple docker containers for our development and staging environments. However, sometimes the container stops running and we only realize it when we try to access the web server. To avoid this problem, we can use the following steps to monitor the containers and get notified if they stop.

To monitor the container status and send email alerts, we will create a script.

Check Docker Container is running using the below command

    docker ps | grep <container-name>

This command will display a list of running containers, and the grep command will filter the output to only show the container we’re looking for.
If the container is not running, we can use a shell script to send email using the mail command.

Here’s an example script: `container-status-check.sh`

https://gist.github.com/kananinirav/9afce4009e82a0a775adbdecd7e3cb86

Change `<container-name>` and `<email-to>` accordingly. You can also add a line to the script that will start the container if it is not running, after sending the email alert. Run the script manually using `bash container-status-check.sh` command.

To use the mail command, you need to have a mail server installed on your EC2 instance. If you don’t have a mail server, you can use a third-party email service like Gmail, Amazon SES, etc…Here we are using Gmail for mail sending.

You will need to configure your EC2 instance to use your email service provider’s SMTP server and provide SMTP credentials (username and password). Once you’ve done that, you can use the mail command to send email from your script.

Follow these steps to set up the mail and cron job:

- **Enable Less Secure Apps:** First, you need to enable **[less secure apps](https://support.google.com/a/answer/6260879?hl=en)** in your Gmail account settings. This allows the mail command to connect to Gmail’s SMTP server using your Gmail credentials. To enable less secure apps, go to your Google Account settings, click on **Security**, and toggle on **Less secure app access**. You can generate Gmail application-specific password.

- Install the **[mailutils](https://mailutils.org/manual/mailutils.html)** and **[ssmtp](https://wiki.archlinux.org/title/SSMTP)** package: The mail command is part of the mailutils package, but to use Gmail’s SMTP server, you need to install the ssmtp package, which provides a simpler interface to send mail using SMTP. To install ssmtp, run the following command:

    sudo apt-get install mailutils ssmtp

- **Configure ssmtp:** Once ssmtp is installed, you need to configure it to use your Gmail account. To do this, edit the `/etc/ssmtp/ssmtp.conf` file and add the following lines at the end:
    ```
    root=<your-gmail-address>
    mailhub=smtp.gmail.com:587
    AuthUser=<your-gmail-address>
    AuthPass=<your-gmail-password>
    UseSTARTTLS=YES
    ```

- **Test the mail command:** To test if the mail command is working, run the following command:

      echo "This is a test email." | mail -s "Test email" <email-address>

- Now we will set up a cron job that will run this script every 5 minutes. Install the cron package using the below command

      sudo apt-get install cron

- Once the installation is complete, start the cron service with the following command:
    
        sudo systemctl start cron

- Verify that the cron service is running with the following command:

      sudo systemctl status cron

- If the service is running, you should see a message that says `Active: active (running)`.

- To create a new cron job, use the following command:

      crontab -e

- This will open the cron configuration file in the default editor.

- Add a new line to the file with the following format: [https://crontab.guru/#*/5_*_*_*_*](https://crontab.guru/#*/5_*_*_*_*)

      */5 * * * * /path/to/container-status-check.sh

- This will run the script every 5 minutes. Adjust the interval as needed. Save and exit the file.
Cron will now run the command at the specified schedule. You can verify that the job is running with the following command:

      crontab -l

This will list all of the cron jobs currently configured for the user.

**Note:** It is important to make sure that any commands or scripts that are run via cron have the correct permissions and are located in the correct directories. Also, be careful when scheduling frequent or resource-intensive jobs, as they could impact the performance of your system.

In this blog, you have learned how to create a simple script that checks the status of your docker containers on EC2 and sends email alerts if they stop running. You have also learned how to run the script periodically using cron jobs. This will help you avoid downtime and ensure optimal performance of your environments. You can now monitor and alert your docker containers on EC2 in less than 5 minutes.

If You are using [Medium](https://medium.com/) Please support and follow me for interesting articles. **[Medium Profile](https://medium.com/@kanani-nirav)**

**If this guide has been helpful to you and your team please share it with others!**