garbage collector for docker
#!/bin/bash
# Remove exited or Dead containers
docker rm $(docker ps -a | grep -E 'Exited|Dead' | awk '{ print $1 }')
# Remove none images
docker rmi $(docker images | grep '<none>' | awk '{ print $3 }')
# Remove unused images
used_images=$(docker ps | tail -n +2 | awk '{ print $2 }')
all_images=$(docker images | grep -v '<none>' | tail -n +2 | awk '{ if ($2 =="latest") b=$1; else b=$1":"$2; print b }')
for line in $all_images; do
if [[ ! $used_images =~ $line ]]; then
docker rmi "$line"
fi
done