Why It's Good to Exercise your Docker Containers (At Least Once a Day)..


This is the visual output of a number of containers.
- Each one was stopped and backed up then restarted. As it ran you can see the CPU RAM requirement significantly dropped!
A simple script as written by Grok 4 Expert can do this automatically daily.
#!/bin/bash
# Ensure script is run as root
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
# Get list of running container IDs
containers=$(docker ps -q)
# Check if there are any running containers
if [ -z "$containers" ]; then
echo "No running containers found"
exit 0
fi
# Iterate through each container
for container_id in $containers; do
# Get container name for display
container_name=$(docker inspect --format '{{.Name}}' "$container_id" | sed 's/^\///')
echo "Processing container: $container_name ($container_id)"
# Stop the container
echo "Stopping container..."
docker stop "$container_id"
# Restart the container
echo "Restarting container..."
docker start "$container_id"
echo "Container $container_name processed"
echo "------------------------"
done
echo "All containers stopped and restarted"
The instructions to add this to cron are as follows:
To schedule the docker_stop_restart.sh
script to run once a day via cron, you can add a cron job. Below, I'll provide instructions to set up the cron job and create a Task Schedule for the requested daily execution.
Instructions to Schedule the Script with Cron
Save the Script: Ensure the docker_stop_restart.sh
script is saved, for example, at /usr/local/bin/docker_stop_restart.sh
, and make it executable:
chmod +x /usr/local/bin/docker_stop_restart.sh
Edit the Crontab: Open the crontab file for the root user (since the script requires root privileges):
sudo crontab -e
Add the Cron Job: Add a line to schedule the script to run daily at a specific time, e.g., 2:00 AM. Use the following format:
0 2 * * * /usr/local/bin/docker_stop_restart.sh >> /var/log/docker_stop_restart.log 2>&1
0 2 * * *
: Runs the script daily at 2:00 AM.>> /var/log/docker_stop_restart.log 2>&1
: Logs output and errors to a file for troubleshooting.
Save and Exit: Save the crontab file (e.g., in vi
, press :wq
and Enter).
Verify the Cron Job: Check that the cron job is set up correctly:
sudo crontab -l
Ensure Cron is Running: Verify that the cron service is active:
sudo systemctl status cron
If it’s not running, start it:
sudo systemctl start cron
sudo systemctl enable cron
Task Schedule
Here’s the Task Schedule to reflect the daily execution of the script:
{
"name": "Daily Docker Stop Restart",
"prompt": "Run the docker_stop_restart.sh script to stop and restart each Docker container one at a time.",
"cadence": "daily",
"time_of_day": "02:00",
"day_of_week": 1,
"day_of_month": 1,
"day_of_year": 1
}
Notes
- The script will run as root, matching the requirement in
docker_stop_restart.sh
. - The log file (
/var/log/docker_stop_restart.log
) helps track execution and troubleshoot issues. - Adjust the
time_of_day
in the Task Schedule or cron job to your preferred time. - Ensure the script path in the cron job matches where you saved
docker_stop_restart.sh
.
Let me know if you need help with any part of this setup!