Skip to content

Instantly share code, notes, and snippets.

@sethbergman
Forked from mmrko/docker-prune.sh
Created March 29, 2019 23:44
Show Gist options
  • Save sethbergman/cb0f1f700b1f6474b9738191055c9fb7 to your computer and use it in GitHub Desktop.
Save sethbergman/cb0f1f700b1f6474b9738191055c9fb7 to your computer and use it in GitHub Desktop.
Prune stale Docker containers, images & volumes (Bash) (versions < 1.13.0)
#!/usr/bin/env bash
stale_images=`docker images --no-trunc --quiet --filter "dangling=true"`
stale_containers=`docker ps --no-trunc --quiet --filter "status=exited"`
stale_volumes=`docker volume ls --quiet --filter "dangling=true"`
stale_images_count=`echo "$stale_images" | sed '/^\s*$/d' | wc -l | xargs`
stale_containers_count=`echo "$stale_containers" | sed '/^\s*$/d' | wc -l | xargs`
stale_volumes_count=`echo "$stale_volumes" | sed '/^\s*$/d' | wc -l | xargs`
echo "Removing stale containers..."
docker rm $stale_containers 2>/dev/null || true
echo "Removing stale images..."
docker rmi $stale_images 2>/dev/null || true
echo "Removing stale volumes..."
docker volume rm $stale_volumes 2>/dev/null || true
active_images=`echo "$(docker images --no-trunc --quiet --filter 'dangling=false')" | sed '/^\s*$/d' | wc -l | xargs`
active_containers=`echo "$(docker ps --no-trunc --quiet --filter 'status=running')" | sed '/^\s*$/d' | wc -l | xargs`
active_volumes=`echo "$(docker volume ls --quiet --filter 'dangling=false')" | sed '/^\s*$/d' | wc -l | xargs`
echo "Removed $stale_images_count stale images ($active_images active)."
echo "Removed $stale_containers_count stale containers ($active_containers active)."
echo "Removed $stale_volumes_count stale volumes ($active_volumes active)."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment