Skip to content

Instantly share code, notes, and snippets.

@mycargus
Created April 13, 2016 20:04
Show Gist options
  • Save mycargus/0ee7f05687351b92e8696a6192e722ec to your computer and use it in GitHub Desktop.
Save mycargus/0ee7f05687351b92e8696a6192e722ec to your computer and use it in GitHub Desktop.
Use this to cleanup your docker-machine host before or when it runs out of space :)
# Purpose: Remove all non-running images and containers
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment