From f3dbd5ce4f7f3c14f55e8283e21b0ee20f83630e Mon Sep 17 00:00:00 2001 From: VinneyJ Date: Wed, 26 Jun 2024 15:46:20 +0300 Subject: [PATCH 1/6] feat: cron job for pruning docker images --- docker-cronjob/README.md | 89 +++++++++++++++++++++++++++ docker-cronjob/prune_docker_images.sh | 2 + 2 files changed, 91 insertions(+) create mode 100644 docker-cronjob/README.md create mode 100644 docker-cronjob/prune_docker_images.sh diff --git a/docker-cronjob/README.md b/docker-cronjob/README.md new file mode 100644 index 0000000..d4c5e2d --- /dev/null +++ b/docker-cronjob/README.md @@ -0,0 +1,89 @@ +# 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. Create the Pruning Script + +Create a script to prune Docker images and make it executable. + +Create the Script +Create a new file for the pruning script: + +```sh +sudo nano prune_docker_images.sh +``` + +Add the following content to the script: + +```sh +#!/bin/bash +sudo docker image prune -f +``` + +#### Make the script executable: + +```sh +sudo chmod +x prune_docker_images.sh +``` + +#### Verify Script Execution + +Test the pruning script manually to ensure it works: + +```sh +/usr/local/bin/prune_docker_images.sh +``` + +### 3. 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 > /dev/null 2>&1 +``` + +### 4. Verify the Cron Job + +List the current cron jobs to ensure your job is added: + +```sh +crontab -l +``` + +## Troubleshooting + +Check cron cervice Status to nsure 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-cronjob/prune_docker_images.sh b/docker-cronjob/prune_docker_images.sh new file mode 100644 index 0000000..d5333d5 --- /dev/null +++ b/docker-cronjob/prune_docker_images.sh @@ -0,0 +1,2 @@ +#!/bin/bash +sudo docker image prune -f \ No newline at end of file From 8dd15753d068e4d694fb2ccd4bbe477316d820ab Mon Sep 17 00:00:00 2001 From: VinneyJ Date: Thu, 27 Jun 2024 09:12:57 +0300 Subject: [PATCH 2/6] fix: add new line, remove extra implemented steps in readme --- docker-cronjob/README.md | 35 ++------------------------- docker-cronjob/prune_docker_images.sh | 2 +- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/docker-cronjob/README.md b/docker-cronjob/README.md index d4c5e2d..1537780 100644 --- a/docker-cronjob/README.md +++ b/docker-cronjob/README.md @@ -16,39 +16,8 @@ SSH into your Dev server: ssh azureuser@ui-1.dev.codeforafrica.org ``` -### 2. Create the Pruning Script -Create a script to prune Docker images and make it executable. - -Create the Script -Create a new file for the pruning script: - -```sh -sudo nano prune_docker_images.sh -``` - -Add the following content to the script: - -```sh -#!/bin/bash -sudo docker image prune -f -``` - -#### Make the script executable: - -```sh -sudo chmod +x prune_docker_images.sh -``` - -#### Verify Script Execution - -Test the pruning script manually to ensure it works: - -```sh -/usr/local/bin/prune_docker_images.sh -``` - -### 3. Set Up the Cron Job +### 2. Set Up the Cron Job Edit the cron table to schedule the pruning job every 4 hours. @@ -64,7 +33,7 @@ Add the following line to schedule the job: 0 */4 * * * /home/azureuser/prune_docker_images.sh > /dev/null 2>&1 ``` -### 4. Verify the Cron Job +### 3. Verify the Cron Job List the current cron jobs to ensure your job is added: diff --git a/docker-cronjob/prune_docker_images.sh b/docker-cronjob/prune_docker_images.sh index d5333d5..cb93c2f 100644 --- a/docker-cronjob/prune_docker_images.sh +++ b/docker-cronjob/prune_docker_images.sh @@ -1,2 +1,2 @@ #!/bin/bash -sudo docker image prune -f \ No newline at end of file +sudo docker image prune -f From 82b7f4a29880da9b5130e8d42ee53e3b2e54ab20 Mon Sep 17 00:00:00 2001 From: VinneyJ Date: Thu, 27 Jun 2024 11:48:14 +0300 Subject: [PATCH 3/6] fix: prune everything, detailed logging, improve readme --- docker-cronjob/prune_docker_images.sh | 2 -- {docker-cronjob => docker-prune-cron}/README.md | 8 +++++++- docker-prune-cron/prune_docker_images.sh | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) delete mode 100644 docker-cronjob/prune_docker_images.sh rename {docker-cronjob => docker-prune-cron}/README.md (89%) create mode 100644 docker-prune-cron/prune_docker_images.sh diff --git a/docker-cronjob/prune_docker_images.sh b/docker-cronjob/prune_docker_images.sh deleted file mode 100644 index cb93c2f..0000000 --- a/docker-cronjob/prune_docker_images.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -sudo docker image prune -f diff --git a/docker-cronjob/README.md b/docker-prune-cron/README.md similarity index 89% rename from docker-cronjob/README.md rename to docker-prune-cron/README.md index 1537780..1bcfad1 100644 --- a/docker-cronjob/README.md +++ b/docker-prune-cron/README.md @@ -30,7 +30,7 @@ crontab -e Add the following line to schedule the job: ``` -0 */4 * * * /home/azureuser/prune_docker_images.sh > /dev/null 2>&1 +0 */4 * * * /home/azureuser/prune_docker_images.sh ``` ### 3. Verify the Cron Job @@ -40,6 +40,12 @@ 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.log +``` ## Troubleshooting diff --git a/docker-prune-cron/prune_docker_images.sh b/docker-prune-cron/prune_docker_images.sh new file mode 100644 index 0000000..0f288f7 --- /dev/null +++ b/docker-prune-cron/prune_docker_images.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +set -o errexit + +LOGFILE="$HOME/docker_prune.log" + +{ + echo "Starting Docker system prune at $(date)" + sudo /usr/bin/docker system prune -f + echo "Pruned Docker objects successfully" + + echo "Starting Docker volume prune at $(date)" + sudo /usr/bin/docker system prune --volumes -f + echo "Pruned Docker volumes successfully" + echo "Completed at $(date)" +} >> "$LOGFILE" 2>&1 From ce221848af69a5225b99d96de2ae739edaedc30b Mon Sep 17 00:00:00 2001 From: VinneyJ Date: Thu, 27 Jun 2024 11:52:23 +0300 Subject: [PATCH 4/6] fix: typo --- docker-prune-cron/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-prune-cron/README.md b/docker-prune-cron/README.md index 1bcfad1..efdba94 100644 --- a/docker-prune-cron/README.md +++ b/docker-prune-cron/README.md @@ -49,7 +49,7 @@ cat docker_prune.log ## Troubleshooting -Check cron cervice Status to nsure the cron service is running: +Check cron service Status to ensure the cron service is running: ```sh systemctl status cron From 280aee7422119888614f2ae1ef551fdfa709b091 Mon Sep 17 00:00:00 2001 From: VinneyJ Date: Thu, 27 Jun 2024 11:53:47 +0300 Subject: [PATCH 5/6] fix: typo --- docker-prune-cron/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-prune-cron/README.md b/docker-prune-cron/README.md index efdba94..51df9e6 100644 --- a/docker-prune-cron/README.md +++ b/docker-prune-cron/README.md @@ -49,7 +49,7 @@ cat docker_prune.log ## Troubleshooting -Check cron service Status to ensure the cron service is running: +Check cron service status to ensure the cron service is running: ```sh systemctl status cron From aeff5bd46fff62b2cf79728e8eb3bbea6c1dcf8c Mon Sep 17 00:00:00 2001 From: VinneyJ Date: Thu, 27 Jun 2024 17:33:49 +0300 Subject: [PATCH 6/6] fix: naming, remove redundancy, date to ISO 8061 format. --- docker-prune-cron/README.md | 2 +- docker-prune-cron/docker_prune_cron.sh | 14 ++++++++++++++ docker-prune-cron/prune_docker_images.sh | 16 ---------------- 3 files changed, 15 insertions(+), 17 deletions(-) create mode 100644 docker-prune-cron/docker_prune_cron.sh delete mode 100644 docker-prune-cron/prune_docker_images.sh diff --git a/docker-prune-cron/README.md b/docker-prune-cron/README.md index 51df9e6..b195a54 100644 --- a/docker-prune-cron/README.md +++ b/docker-prune-cron/README.md @@ -44,7 +44,7 @@ You can also check the logged output: ```sh -cat docker_prune.log +cat docker_prune_cron.log ``` ## Troubleshooting 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 diff --git a/docker-prune-cron/prune_docker_images.sh b/docker-prune-cron/prune_docker_images.sh deleted file mode 100644 index 0f288f7..0000000 --- a/docker-prune-cron/prune_docker_images.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -o errexit - -LOGFILE="$HOME/docker_prune.log" - -{ - echo "Starting Docker system prune at $(date)" - sudo /usr/bin/docker system prune -f - echo "Pruned Docker objects successfully" - - echo "Starting Docker volume prune at $(date)" - sudo /usr/bin/docker system prune --volumes -f - echo "Pruned Docker volumes successfully" - echo "Completed at $(date)" -} >> "$LOGFILE" 2>&1