Last active
February 20, 2016 14:26
-
-
Save cdpb/a637932a4af906b0dbf7 to your computer and use it in GitHub Desktop.
Automatic Docker Clean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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