BASH
Clean Up Stale Docker Images, Containers, and Volumes
Free up disk space by automating the removal of unused Docker containers, images, and volumes with a simple Bash script. Essential for development and production environments.
#!/bin/bash
echo "Starting Docker cleanup..."
# Remove all stopped containers
echo "Removing stopped Docker containers..."
docker container prune -f
# Remove all dangling images (images not associated with any container)
echo "Removing dangling Docker images..."
docker image prune -f
# Remove all dangling volumes
echo "Removing dangling Docker volumes..."
docker volume prune -f
# Remove unused networks
echo "Removing unused Docker networks..."
docker network prune -f
# Optional: Remove all images not used by any container (be careful with this!)
# echo "Removing all unused Docker images (not just dangling)..."
# docker image prune -a -f
echo "Docker cleanup complete."
How it works: This script helps maintain a clean Docker environment by removing unused resources. It systematically prunes stopped containers, dangling images (images without an associated container), dangling volumes, and unused networks using `docker prune` commands. This is highly useful for developers to reclaim disk space, especially in CI/CD pipelines or local development machines where many temporary containers and images are created.