Created
May 20, 2015 16:42
-
-
Save cgswong/e880f3a9423ffa2932c5 to your computer and use it in GitHub Desktop.
Docker container cleanup
This file contains 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 | |
echo "Removing exited docker containers..." | |
docker rm -v $(docker ps -a -f status=exited -q) | |
echo "Removing untagged images..." | |
docker rmi $(docker images --no-trunc | grep "<none>" | awk '{print $3}') | |
echo "Removing unused docker images" | |
images=($(docker images | awk '{print $1":"$2}')) | |
containers=($(docker ps -a | awk '{print $2}')) | |
containers_reg=" ${containers[*]} " | |
remove=() | |
for item in ${images[@]}; do | |
if [[ ! $containers_reg =~ " $item " ]]; then | |
remove+=($item) | |
fi | |
done | |
remove_images=" ${remove[*]} " | |
docker rmi ${remove_images} | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment