Last active
April 24, 2017 22:36
-
-
Save r3dey3/490cadc423ce69f30bf6726c9de568d7 to your computer and use it in GitHub Desktop.
docker-cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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