How to Automatically Restart a Docker Instance by Checking if a Website is Responsive Using Cron
Managing web services hosted on Docker containers can sometimes require ensuring that your site is running smoothly. If the website stops responding or fails to return the expected webpage text, restarting the Docker instance can help resolve the issue. This blog post will guide you through creating an automated system using a shell script and a cron job to monitor the website and restart the Docker instance if it becomes unresponsive.
This guide is designed for beginners and will walk you through all the steps to set up this process on a Linux server. Many thanks to mr. s!!!!u for guiding through this!
Prerequisites:
- Basic knowledge of the Linux command line
- Docker and Docker Compose installed on the server
- A web service running on Docker, exposed on a specific port (e.g.,
8000
), assume your IP address is 20X.17X.16X.XXX or something similar. - SSH access to your server
Step 1: Create a Shell Script to Check Website Status and Restart Docker
The first step is to create a shell script that will:
- Use
curl
to check if the website is responding. - If the website is unresponsive, the script will log the Docker instance's status and restart it.
1.1. Create the Shell Script
Open a terminal on your server and create the shell script. Let’s assume you want to place the script in the /root
directory.
Run this command to create the file:
Now, add the following code to the file:
1.2. Explanation of the Script:
curl -s -k https://20X.17X.16X.XXX:8000 > /dev/null
: This line uses thecurl
command to silently (-s
) attempt to access the website athttps://20X.17X.16X.XXX:8000
. The-k
option ignores SSL certificate verification, which is useful if you have a self-signed certificate.$?
: This is the exit status of the last command. Ifcurl
fails to connect to the website (i.e., the website is unresponsive), the exit status will not be0
.docker compose logs > running.logs
: This logs the current status of the Docker containers to a file calledrunning.logs
before taking any action.sudo docker compose down && sudo docker compose up -d
: This brings down the Docker containers and restarts them in detached mode (-d
).
1.3. Make the Script Executable:
After saving the file (Ctrl + X
, then Y
, and press Enter
), make the script executable with the following command:
Step 2: Set Up a Cron Job to Run the Script Regularly
Now that you have the shell script, you need to schedule it to run automatically at regular intervals using a cron job.
2.1. Open the Crontab
You can set up cron jobs by editing the crontab file for the root user (or the appropriate user running the Docker services). Open the crontab file with:
2.2. Add the Cron Job
To run the script every 5 minutes, add the following line at the end of the crontab file:
Explanation of the Cron Job:
*/5 * * * *
: This cron expression means "run the command every 5 minutes."*/5
: Every 5 minutes.- The remaining
* * * *
indicate "every hour, every day, every month, and every day of the week."
/root/restart_webejs_server.sh
: This is the path to the shell script we created.>> /root/restart_webejs_server.log 2>&1
: This redirects both the standard output (stdout
) and the standard error (stderr
) to a log file calledrestart_webejs_server.log
. This will allow you to review what happened during the script execution.
2.3. Save the Crontab
After adding the cron job,
save and exit the crontab (
: + wq
and press Enter
). w for write, q for quitStep 3: Testing the Setup
To test if everything is set up correctly, you can manually run the script to see how it behaves when the website is unresponsive. You can do this by stopping the Docker service and executing the script:
After running the script, you should see the Docker containers being restarted automatically. You can check the logs in /root/restart_webejs_server.log
to verify.
Step 4: Monitor the Logs
Your script will generate logs each time it runs, which can be helpful to track the performance of your service and catch any issues. To view the log file, use:
You can clear the log file periodically by deleting it with the following command:
Or, you can set up a cron job to automatically clear the logs at regular intervals if needed.
Conclusion
In this guide, we walked through how to automatically restart a Docker instance by checking if a website is responsive. With a simple shell script and cron job, you can monitor your service and ensure it stays online with minimal manual intervention.
This is a simple but effective way to maintain high uptime for your web services, and it's easy enough for anyone to set up with basic command-line skills. As you gain more experience, you can expand this system by adding more sophisticated checks, notifications, or integrating it with other monitoring tools.
Let me know if you have any questions or issues while setting this up! Happy monitoring!
No comments:
Post a Comment