Skip to content

Instantly share code, notes, and snippets.

@cdpb
Last active February 20, 2016 14:26
Show Gist options
  • Save cdpb/a637932a4af906b0dbf7 to your computer and use it in GitHub Desktop.
Save cdpb/a637932a4af906b0dbf7 to your computer and use it in GitHub Desktop.
Automatic Docker Clean
#!/bin/bash
echo "clean containers ..."
docker ps --filter status=exited -aq | xargs --no-run-if-empty docker rm -vf
echo "clean images ..."
docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi -f
docker images | grep old | awk '{ print $1 " " $2 }' | while read LINE
do
IMAGE=$(echo $LINE | cut -d' ' -f1)
TAG=$(echo $LINE | cut -d' ' -f2)
d1=$(date -d $TAG +%s)
d2=$(date +%s)
if [[ $(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) -gt 30 ]]; then
docker rmi -f $IMAGE:$TAG
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment