Thursday 17 Jul 2025
Auto Docker Cleaner
I have been running Docker without issue. However, one day I got an error during an apt upgrade. The error occured due to running out of free space.
When you're working with Docker, over time, your system can accumulate a lot of unused containers, images, volumes, and networks that can take up space and slow down your workflow. Rather than manually cleaning up these resources, you can automate the process with a bash script. Below is a simple script that helps you keep your Docker environment lean and clean.
Create a bash file as below, set permissions to chmod x+ /location/file.sh
#!/bin/bash
# Remove stopped containers
sudo docker container prune -f
# Remove unused images
sudo docker image prune -a -f
# Remove unused volumes
sudo docker volume prune -f
# Remove unused networks
sudo docker network prune -f
# Clean up any leftover system resources
sudo docker system prune -f
Conclusion
Docker is a powerful tool, but managing its resources can sometimes feel like a chore. With this bash script, you can streamline the cleanup process and ensure your system stays lean and efficient. Set it up as a scheduled task if you'd like it to run periodically, or simply run it manually after a heavy session of Docker use. Either way, this will save you time and disk space in the long run.
Backlinks:
index
Journal:Index
Journal:2025:07