Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleaner for docker containers
#!/bin/bash
set -o errexit
echo "Removing exited docker containers..."
docker ps -a -f status=exited | grep ^data | awk '{print $1}' | xargs -r docker rm -v
#docker ps -a -f status=exited -q | xargs -r docker rm -v
echo "Removing dangling images..."
docker images --no-trunc -q -f dangling=true | xargs -r docker rmi
echo "Removing unused docker images"
images=($(docker images | tail -n +2 | awk '{print $1":"$2}'))
containers=($(docker ps -a | tail -n +2 | awk '{print $2}'))
containers_reg=" ${containers[*]} "
remove=()
for item in ${images[@]}; do
if [[ ! $containers_reg =~ " $item " ]]; then
remove+=($item)
fi
done
remove_images=" ${remove[*]} "
echo ${remove_images} | xargs -r docker rmi
echo "Done"
@egel
Copy link
Author

egel commented Aug 12, 2015

To clean all unused containers run (it won't remove any important docker containers):

wget https://gist.githubusercontent.com/egel/1d392601f3016fef8b8a/raw/e54c4224366c301928e565add7f34197327ab1f1/cleanup_docker.sh -q -O - | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment