A modular bash script to find and remove Docker resources (images, containers, volumes, and networks) matching a specified search term.
- Search & Destroy: Find all Docker resources matching a keyword
- Comprehensive Cleanup: Handles images, containers, volumes, and networks
- Dry Run Mode: Preview what would be deleted without making changes
- Auto-Confirm Mode: Skip confirmation prompt for scripted/automated use
- Optional Logging: Save detailed log files for auditing when needed
docker-resources-liberator/
├── liberate.sh # Main entry point
├── src/ # Modular source files
│ ├── config.sh # Configuration & global variables
│ ├── helpers.sh # Output formatting & logging
│ ├── resources.sh # System resource tracking
│ ├── discovery.sh # Docker resource discovery
│ └── cleanup.sh # Docker resource cleanup
├── tests/ # Unit tests (bats)
├── logs/ # Log files (when --log is used)
└── README.md # This file
-
Clone or copy the project to your desired location:
git clone https://github.com/MussaCharles/docker-resources-liberator.git
-
Make the script executable:
chmod +x ~/docker-resources-liberator/liberate.sh
./liberate.sh <search_term> [options]| Option | Description |
|---|---|
-y, --yes |
Skip confirmation prompt (auto-confirm deletion) |
-d, --dry-run |
Show what would be deleted without actually deleting |
-l, --log |
Save output to a log file in logs/ directory |
-h, --help |
Show help message |
# Find and interactively delete resources matching 'myproject'
./liberate.sh myproject
# Preview what would be deleted (no actual deletion)
./liberate.sh myproject --dry-run
# Auto-confirm deletion (useful for scripts)
./liberate.sh myproject -y
# Save a log file for auditing
./liberate.sh myproject --log
# Combine options
./liberate.sh myproject --dry-run -y --logThe script searches for and removes:
| Resource Type | Search Method |
|---|---|
| Images | Repository name or tag containing the search term |
| Containers | Container name containing the search term |
| Volumes | Volume name containing the search term |
| Networks | Network name containing the search term |
Resources are removed in a specific order to handle dependencies:
- Containers (first, as they may depend on images/volumes/networks)
- Images (after containers that use them are removed)
- Volumes (after containers that mount them are removed)
- Networks (after containers that use them are removed)
Logging is disabled by default. Use -l or --log to enable it.
When enabled, log files are created in the logs/ directory with the naming format:
<search_term>_YYYYMMDD_HHMMSS.log
- Timestamp and host information
- System resources before cleanup (disk, memory, Docker usage)
- List of resources found
- Deletion results (success/failure for each resource)
- System resources after cleanup
- Resource comparison (space freed)
./liberate.sh myproject --log
# Creates: ~/docker-resources-liberator/logs/myproject_20241215_143022.log- Disk Usage: Root filesystem and Docker filesystem (if separate)
- Memory Usage: Total, used, free, available RAM
- Docker Usage: Images, containers, volumes size breakdown
- Space Freed: Calculated difference after cleanup
- Confirmation Prompt: By default, requires explicit
yconfirmation - Dry Run Mode: Test what would happen without making changes
- Case-Insensitive Search: Catches variations in naming
- Optional Logging: Enable audit trail when needed with
--log - Error Handling: Reports failed deletions without stopping
- Bash 4.0+
- Docker CLI installed and accessible
- Standard Unix utilities:
awk,grep,sed,df - Works on both macOS and Linux
- Run with
--dry-runfirst to preview what will be deleted - Use
--logwhen you need an audit trail - Use
docker system pruneafter running for additional cleanup - Create wrapper scripts for projects you clean frequently
On macOS with Docker Desktop, volume sizes are retrieved from Docker's internal data. If sizes show as "N/A", ensure Docker is running.
- Check if containers are still running (they'll be force-stopped)
- Some resources may be in use by other containers not matching the search term
Run docker images, docker ps -a, docker volume ls, and docker network ls to see all resources and identify the correct search term.
This project is licensed under the MIT License.
Contributions are welcome! Please read our Contributing Guidelines before submitting a pull request.
This project follows the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.
Need help? Check out our Support Guide for common issues and how to get assistance.
Found a vulnerability? See our Security Policy for reporting guidelines.
This project was developed with AI assistance, primarily using Claude Code. While every effort has been made to review and test the code, users are encouraged to verify the behavior in their own environments. If you find any issues or have suggestions for improvement, contributions are welcome! See our Contributing Guidelines.