Haraball.no

Cleaning used Docker containers with a cronjob

2016-10-19

Following this guide, I created a small crontab job that cleans out Docker containers with status "Exited" every 10 minutes to avoid having them fill up my disk.

I created a script in ~/cronjobs/clean_docker.sh which does:

#!/bin/bash
docker rm $(docker ps -q -f status=exited) >/dev/null 2>&1
docker rm $(docker ps -q -f status=created) >/dev/null 2>&1

Then I ran crontab -e in my terminal, and added the line

*/10 * * * *  ~/cronjobs/clean_docker.sh >> ~/cronjobs/logs/clean_docker.log 2>&1

to the file.

This will do the cleaning and log any output to a log file.