Skip to content

Instantly share code, notes, and snippets.

@Komorebi-E
Last active June 11, 2019 09:04
Show Gist options
  • Save Komorebi-E/4dabf3330c00ed6eb7205d9a21e4cb4c to your computer and use it in GitHub Desktop.
Save Komorebi-E/4dabf3330c00ed6eb7205d9a21e4cb4c to your computer and use it in GitHub Desktop.
Clean up docker containers, images, networks - Stops all containers, force remove containers and their images, prune networks
#!/bin/bash
# Remove any docker containers that are running:
# stop, remove them, their images, volumes and networks.
if $(docker ps -a -q | grep -qE "[a-z0-9]") || $(docker images | grep -qE "[a-z0-9]"); then
if [[ $(docker ps -a -q | head -c1 | wc -c) -ne 0 ]]; then
docker ps -a -q | xargs docker stop
fi
if [[ $(docker ps -a -q | head -c1 | wc -c) -ne 0 ]]; then
docker rm -f $(docker ps -a -q)
fi
docker rmi -f $(docker images -q)
# remove volumes
if [[ $(docker volume ls -q | head -c1 | wc -c) -ne 0 ]]; then
docker volume rm -f $(docker volume ls -q)
fi
echo -e "\nPrune networks (no confirmation)\n"
docker network prune -f
echo -e "\n--- Container States ---\n"
docker ps -a
echo -e "\n--- Images ---\n"
docker images
echo -e "\n--- Volumes ---\n"
docker volume ls
echo -e "\n--- Networks ---\n"
docker network ls
else
echo -e "\nNo running or stopped containers\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment