Skip to content

feat: cron job for pruning docker images #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docker-prune-cron/README.md
Original file line number Diff line number Diff line change
@@ -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 [email protected]
```


### 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally you don't want scripts laying around in a home folder. You usually put them in say ~/.local/bin, etc.

But since we're going to develop this further to be part of Dokku plugin ecosystem, I guess we can leave it here for now.

```

### 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
```
14 changes: 14 additions & 0 deletions docker-prune-cron/docker_prune_cron.sh
Original file line number Diff line number Diff line change
@@ -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