Setup a Cron job to automatically Prune all unused docker images, volumes and networks on a daily basis to save you time ensuring you never run out of disk space on your VPS.
Before you begin
Having deployed Watchtower in the previous episodes of this series, Docker will now automatically pull and update all of your containers that exist within your docker and docker-compose ecosystem when a new container is released by the author. Unfortunately a downside of this is that your VPS attached storage can fill up quickly with old container images if you have a lot of containers that update fairly frequently.
Docker has a fantastic 'prune' command to tell it to cleanup all unused docker containers but unfortunately this is not something that it does by itself so you need to login every now and then and tell it to.
Let's check out the docker prune command:
Just a word of warning here: this command will delete dangling and unused images (Unused images are images no longer referenced by any containers), networks that are not used, the build cache and any stopped containers you might have.
If this is the first time you've run this command, you may have a few gigs of data that gets reclaimed, for me this was 0 bytes as I had previously run the command.
I'm all for saving ourselves times, so let's automate this command 👍.
The simplest way to do this is to run a cronjob daily to execute out prune command.
Let's break this down a little bit to understand what's happening here:
- Docker system prune - We're asking Docker to prune unused containers.
- -af - We've conjoined two flags here. By Appending the -a flag forces docker to look for not just dangling containers but all containers that are not in use anymore. The -f flag just prevents the confirmation prompt which typically prompts the user to confirm if they are happing to confirm with the pruning.
Just a word of warning here: 'unused' means "images not referenced by any container": be careful before using -a!
- --filter - Adding a filter allows us to specify the age of images that we wish to delete. In this case it's any images that are older than 30 days or 1 month. We're using some basic math's in bash here as bash only supports h (Hours). So let's calculate 30*24 hours which gives us 1 month of time in hours which typically limits the number of containers to a few gigs. You can shorten this time should you have very limited disk space on your VPS.
lets create a new file in the Cron file to house our command:
Once the file opens add the prune command we created and inform the OS it's a bash file
Once you've added the command, hit Ctrl + O and then Ctrl + X to save and close the file
Now that we've created the file we need to tell the operating system that it needs to be executable with the following command:
And that's it. Docker will now execute this command on a daily basis.
If you want to test out to see if it will execute correctly next time the cronjob runs, you can force the daily Cron register to run with the following command: