Docker Host Cleanup Routine (cron?)

Just wondering if someone has a nice and safe clean script the run regularly (cron or similar) for cleaning away unused docker images etc?

I’m running a nightly compose down, backup and compose up, so then it seems every now and then my filesystem is running close to full, so I login and run
docker system prune -a --volumes

Not sure how safe it is, I just make sure all my required containers are running fine before I run that command, as it won’t delete anything that’s running … :sweat_smile:

And then I run something like:
journalctl --vacuum-time=2d

Looking to automate the cleanup a bit …

The nightly backup runs something like:

cd /dir/to/docker/files && docker compose down
rsync --stats -azu --delete /docker/data/dir user@host:/backup/dir/
docker compose up -d

Does this operation also pull the newest versions, since there’s so many docker images to prune all the time? :thinking: :nerd_face:

1 Like

No script, but weekly I run a custom docker-compose update script, then when everything is up and running properly I issue your same command

docker system prune -a --volumes

Not pretty or elegant but it works just fine.
Also, I have run this script a few times with good results. But I don’t rely on it, just threw it in for your consideration.

As for your question, as long as you delete the images it the dcup command will pull the latest images. My custom script goes something like this:
cd ~/docker-dir && echo “Updating Docker Name …” && docker compose pull && docker compose up -d


I just docker image prune -f after pull && up -d.

1 Like

And, do you automate this? Or just do it manually? :smiley:

Manually. If I wanted to do it automatically I’d simply use watchtower. The issue is that sometimes containers have breaking changes, and if I automate it, it’d most certainly break at an inconvenient time. If I run it manually at the time convenient for me, I have the time to fix things if they break.