diff --git a/docker-prune-cron/README.md b/docker-prune-cron/README.md new file mode 100644 index 0000000..b195a54 --- /dev/null +++ b/docker-prune-cron/README.md @@ -0,0 +1,64 @@ +# Docker Image Pruning Cron Job for Dev server + +This README provides instructions for setting up a cron job to regularly prune old Docker images on a Dev server. Pruning Docker images helps to manage disk space effectively by removing unused images. + +## Prerequisites + +- SSH access to the Dev server. + +## Steps to Set Up the Cron Job + +### 1. Access the Dev Server + +SSH into your Dev server: + +```sh +ssh azureuser@ui-1.dev.codeforafrica.org +``` + + +### 2. Set Up the Cron Job + +Edit the cron table to schedule the pruning job every 4 hours. + +Open the crontab for editing: + +```sh +crontab -e +``` + +Add the following line to schedule the job: + +``` +0 */4 * * * /home/azureuser/prune_docker_images.sh +``` + +### 3. Verify the Cron Job + +List the current cron jobs to ensure your job is added: + +```sh +crontab -l +``` +You can also check the logged output: + +```sh + +cat docker_prune_cron.log +``` + +## Troubleshooting + +Check cron service status to ensure the cron service is running: + +```sh +systemctl status cron +``` + +#### Check Syslog for Errors + +If the cron job doesn’t seem to run, check the syslog for cron errors: + +```sh +grep CRON /var/log/syslog +``` diff --git a/docker-prune-cron/docker_prune_cron.sh b/docker-prune-cron/docker_prune_cron.sh new file mode 100644 index 0000000..89cd2d1 --- /dev/null +++ b/docker-prune-cron/docker_prune_cron.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -o errexit +set -o nounset + +LOGFILE="$HOME/docker_prune_cron.log" + +current_date=$(date -Iseconds) + +{ + echo "Starting Docker system prune at $current_date" + sudo docker system prune --volumes --force + echo "Completed at $(date -Iseconds)" +} >> "$LOGFILE" 2>&1