Skip to content

Instantly share code, notes, and snippets.

@r3dey3
Last active April 24, 2017 22:36
Show Gist options
  • Save r3dey3/490cadc423ce69f30bf6726c9de568d7 to your computer and use it in GitHub Desktop.
Save r3dey3/490cadc423ce69f30bf6726c9de568d7 to your computer and use it in GitHub Desktop.
docker-cleanup
#!/bin/bash
# Usage:
# docker-cleanup.sh [-y]
#
# When run without -y will print
# all dead or exited containers
# all dangling images (i.e. images without tags that are not used by any container)
# all dangling volumes (all volumes not in use)
#
# When run with -y, it will DELETE
# all dead or exited containers
# all dangling images
#
do_it=false
if [[ $# -ne 0 ]]; then
if [[ "x$1" == "x-y" ]]; then
do_it=true
fi
fi
#change to empty string if not using sudo
# SUDO=
SUDO=sudo
if $do_it; then
$SUDO docker ps -f status=exited -f status=dead -q | xargs -r $SUDO docker rm
$SUDO docker images -f "dangling=true" -q | xargs -r $SUDO docker rmi
else
echo "The following containers will be removed:"
$SUDO docker ps -f status=exited -f status=dead
echo
echo "The following images will be removed; note more may be removed after containers are removed"
$SUDO docker images -f "dangling=true"
echo
echo "The following volumes should be removed with 'docker volume rm'"
$SUDO docker volume ls -qf dangling=true
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment