Skip to content

Instantly share code, notes, and snippets.

@cgswong
Created May 20, 2015 16:42
Show Gist options
  • Save cgswong/e880f3a9423ffa2932c5 to your computer and use it in GitHub Desktop.
Save cgswong/e880f3a9423ffa2932c5 to your computer and use it in GitHub Desktop.
Docker container cleanup
#! /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