Last active
August 29, 2015 14:06
-
-
Save kizbitz/a6244df50fdd7dc95db3 to your computer and use it in GitHub Desktop.
Purge Docker containers/images
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
# Bash | |
function dclean { | |
if [ ! -z "$1" ]; then | |
if [ "$1" = "restart" ]; then | |
sudo service docker restart | |
sleep 3 | |
fi | |
fi | |
export CONTAINERS=`docker ps -a -q` | |
export IMAGES=`docker images -q --filter "dangling=true"` | |
if [ "$CONTAINERS" ]; then | |
echo "Deleting non-running containers:" | |
docker rm `docker ps -a -q` | |
else | |
echo "No existing containers..." | |
fi | |
if [ "$IMAGES" ]; then | |
echo "Deleting dangling images:" | |
docker images -q --filter "dangling=true" | xargs docker rmi | |
else | |
echo "No dangling images..." | |
fi | |
} |
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
# Fish | |
function dclean | |
if [ $argv[1] ] | |
if [ $argv[1] = 'restart' ] | |
sudo service docker restart | |
sleep 3 | |
end | |
end | |
set -l CONTAINERS (docker ps -a | grep Exited | cut -f1 -d ' ') | |
set -l IMAGES (docker images -q --filter "dangling=true") | |
if [ $CONTAINERS[1] ] | |
echo "Deleting non-running containers:" | |
docker rm (docker ps -a | grep Exited | cut -f1 -d ' ') | |
else | |
echo "No existing containers..." | |
end | |
if [ $IMAGES[1] ] | |
echo "Deleting dangling images:" | |
docker images -q --filter "dangling=true" | xargs docker rmi | |
else | |
echo "No dangling images..." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment