Skip to content

Instantly share code, notes, and snippets.

@bigorn0
Created August 25, 2016 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigorn0/5618e65b53191884ba9f37cc0e10bf9c to your computer and use it in GitHub Desktop.
Save bigorn0/5618e65b53191884ba9f37cc0e10bf9c to your computer and use it in GitHub Desktop.
Cleanup Dood
#!/bin/sh
str_in_list() {
str="$1"
shift
list="$@"
if test "${list#*$str}" != "$list"
then
return 0 # $str is in $list
else
return 1 # $str is not in $list
fi
}
# detects containers not referencing the current si-build-context image
get_deletable_containers () {
candidates=$(docker ps -a --filter status=created --filter name=si-build- -q)
containers_to_retain=$(docker ps -a --filter status=created --filter name=si-build- --filter ancestor=si-build-context -q)
for cid in ${candidates} ; do
str_in_list ${cid} ${containers_to_retain} || printf %s\\n "$cid";
done
}
# detects volumes not containing 'data' in their name
get_deletable_volumes () {
candidates=$(docker volume ls --filter dangling=true -q)
# containers_to_retain=$(docker volume ls --filter dangling=true --filter name=.*data.* -q)
containers_to_retain=()
for cid in ${candidates} ; do
str_in_list ${cid} ${containers_to_retain} || printf %s\\n "$cid";
done
}
deletable_containers=$(get_deletable_containers)
deletable_volumes=$(get_deletable_volumes)
echo "deleting containers (including their anonymous volumes)..."
docker rm -v ${deletable_containers}
echo "deleting images..."
docker rmi $(docker images --filter dangling=true -q)
echo "deleting volumes..."
docker volume rm ${deletable_volumes}
#with TeamCity (running in Docker) spawning other builder containers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment