Docker Image cleaning easy automation with cron

- 1 min read

Docker makes it easy to ship your applications and services in containers, so you can run them anywhere. Also true is that the more you work with Docker the easier your VM tends to accumulate a huge number of unused images, containers, and data volumes in your filesystem that clutter the output and consume a lot of precious disk space, eventually leading to crashes.

Docker, in newer versions, gives you all the tools you need to clean up your system from the command line. For this, I created a bash script to initialise a cron-job every day at 8am, so it takes responsibility on your system housekeeping, freeing disk space and keeping your system organized by removing unused Docker images, containers, and volumes.

Docker Image bash script to automate the cleaning

In order to keep my own VMs clean, I use the below script to add the “cleaner” to them:

#!/usr/bin/env bash

# Setup /opt/
cat <<- 'EOF' > /opt/
#!/usr/bin/env bash
echo "========== Docker System Sitting =========="
docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi -f
echo "========== Docker System Cleaned =========="

# Setup cron job for Docker Cleaner
chmod +x /opt/
crontab -l > /tmp/newcron
echo "0 8 * * * /opt/" >> /tmp/newcron
crontab /tmp/newcron
rm /tmp/newcron

Share: Link copied to clipboard


Previous: Los Chinos Nos Copian
Next: Guía de NBN para el inmigrante, en SBS Radio

Where: Home > Technical > Docker Image cleaning easy automation with cron