The Docker Cleanup Utility is a Bash script designed to help manage and clean up Docker resources. It allows you to efficiently remove unused Docker containers, images, volumes, networks, and build cache, reclaiming disk space in your system. Additionally, the script provides disk usage statistics and the ability to selectively clean up Docker components.
This tool is especially useful for developers and DevOps engineers who work with Docker regularly and need to keep their environment lean and clean.
- Remove Stopped Containers: Clean up containers that are no longer running.
- Remove Dangling Images: Delete untagged images that take up space but are not being used.
- Remove Unused Volumes: Clean up unused Docker volumes to reclaim disk space.
- Remove Unused Networks: Delete Docker networks that are no longer in use.
- System Prune: Clean up all unused Docker resources in one go (stopped containers, unused images, volumes, networks).
- Remove Build Cache: Clear the Docker build cache to free up space used during image builds.
- Container, Image, Volume Cleanup by Name or ID: Remove specific containers, images, or volumes by name or ID.
- Log Truncation: Truncate Docker container logs to save disk space.
- Show Docker Disk Usage: View disk usage statistics for Docker images, containers, volumes, and more.
- Disk Space Alert: Get an alert when disk usage exceeds a certain threshold.
- Interactive Menu: Simple text-based interface that guides you through the available cleanup options.
- Confirmation Prompts: Avoid accidental deletion by prompting for user confirmation before destructive actions.
- Dry Run: Option to simulate cleanup actions without actually performing them.
- Docker: Ensure Docker is installed and running on your system.
- Bash: The script is written for Bash, which is available on most Unix-like operating systems (Linux, macOS).
- Permissions: You may need root or Docker group permissions to run some Docker commands (e.g., pruning or removing containers/images).
You can download or copy the script directly into a file on your machine:
curl -o docker_cleanup.sh https://github.com/cricsion/DockerCleanup/main/docker_cleanup.sh
Alternatively, you can copy and paste the script into a new file, e.g., docker_cleanup.sh
.
After downloading or creating the script, make it executable:
chmod +x docker_cleanup.sh
You can now run the script from the command line:
./docker_cleanup.sh
To automatically run the cleanup process on a schedule, you can add the script to cron
(Linux/macOS) or Task Scheduler (Windows). For example, to run the cleanup every Sunday at 3 a.m.:
crontab -e
Add the following line to schedule the cleanup:
0 3 * * SUN /path/to/docker_cleanup.sh
When you run the script, you’ll be presented with an interactive menu to choose from various Docker cleanup actions. Here are the available options:
-
Remove Stopped Containers
Removes all stopped containers to free up space. -
Remove Unused Images (Dangling)
Cleans up untagged images (dangling images), which are no longer needed. -
Remove Unused Volumes
Deletes unused Docker volumes that are taking up space. -
Remove Unused Networks
Cleans up Docker networks that are not being used. -
Remove All Unused Docker Data (System Prune)
Prunes all unused Docker resources, including containers, images, volumes, and networks. Be cautious as this action is irreversible. -
Remove Docker Build Cache
Clears the Docker build cache to free up disk space used during image builds. -
Remove All Docker Data (System Prune -a)
Removes all unused images, including those that aren't dangling, in addition to containers, volumes, and networks. This is a more aggressive cleanup. -
Remove Specific Container
Prompt the user to specify a container by name or ID for removal. -
Remove Specific Image
Prompt the user to specify an image by name or ID for removal. -
Remove Specific Volume
Prompt the user to specify a volume by name or ID for removal. -
Remove Docker Log Files
Truncate the log file for a specified container without deleting the log file itself. This helps reduce disk usage by clearing large log files. -
Show Docker Disk Usage (docker system df)
View detailed disk usage statistics for Docker resources such as images, containers, and volumes. -
Show Disk Space Alert
Alerts if your disk usage exceeds 90%, prompting you to clean up resources. -
Exit
Exit the script without performing any actions.
$ ./docker_cleanup.sh
Docker Cleanup Utility
=======================
Choose an option:
1) Remove stopped containers
2) Remove unused images (dangling)
3) Remove unused volumes
4) Remove unused networks
5) Remove all unused Docker data (system prune)
6) Remove build cache
7) Remove all Docker data (system prune -a)
8) Remove specific container
9) Remove specific image
10) Remove specific volume
11) Remove Docker log files
12) Show Docker disk usage (docker system df)
13) Show disk space alert
14) Exit
=======================
Enter choice [1-14]: 5
Are you sure you want to proceed? (y/n): y
Removing all unused Docker data (system prune)...
- Confirmation Prompts: Before performing destructive actions like pruning Docker resources, the script asks for user confirmation to ensure you don’t accidentally delete important data.
- Dry Run Mode: If you're unsure about what the script will do, you can run it in a dry-run mode to simulate the cleanup process without making any changes.
- Every cleanup action is logged with a timestamp to a
cleanup_history.log
file. This allows you to track past cleanups and troubleshoot if necessary.
Feel free to modify the script for your own needs:
- Cron Jobs: You can schedule the script to run automatically at specific intervals (e.g., weekly or monthly).
- Add More Cleanup Options: The script is highly customizable—add more specific Docker commands as needed for your environment.
- Permissions: If you encounter permission issues, ensure you're running the script with sufficient privileges (e.g., using
sudo
for Docker commands). - No Output: If the script doesn’t show any output for some actions, check Docker's status or ensure the resources (e.g., stopped containers, unused volumes) exist.
Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.
This project is licensed under the MIT License – see the LICENSE file for details.