Docker Image cleaning easy automation with cron

Posted on 3 May 2018 by Alberto Roura.

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

Original article: Docker Image cleaning easy automation with cron.

✉️ Contact Us

Ready to take the next step? Don't wait any longer! If you're interested in learning more about our products and services, or if you have any questions or concerns, email us today.

[email protected]

Our team of experts is standing by, ready to assist you with anything you need. Drop us an email, and we'll take care of the rest.