Skip to content

Instantly share code, notes, and snippets.

@silverbp
Forked from stevenschlansker/gist:51c319cdc7cd2764ce48
Last active August 29, 2015 14:24
Show Gist options
  • Save silverbp/f6040b88f26534897ebf to your computer and use it in GitHub Desktop.
Save silverbp/f6040b88f26534897ebf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Shamelessly stolen from https://gist.github.com/eduardocardoso/82a629882ddb02ab3677
set -o errexit
echo "Removing exited docker containers..."
docker ps -a -f status=exited -q | xargs -r docker rm -v
echo "Removing untagged images..."
docker images --no-trunc | grep "<none>" | awk '{print $3}' | xargs -r docker rmi
echo "Removing unused docker images"
images=($(docker images | tail -n +2 | awk '{gsub("docker.io/","",$1); 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"
@silverbp
Copy link
Author

silverbp commented Jul 9, 2015

if you have a custom docker registry you have to add the gsub to get rid of the docker.io/ out of the docker images.

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