Created
October 4, 2017 12:32
-
-
Save fvztdk/18288a8a3d6726dec7a1f9599948c275 to your computer and use it in GitHub Desktop.
Clear older than X weeks docker images
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 | |
#use this to remove older docker images left on CI machine, max_week_size represent the number of weeks that should be kept | |
max_week_size=4 | |
docker images | awk 'NR>1 {print $0}' | while read line; do | |
# echo $line | |
id_img=$(echo $line | awk '{print $3}') | |
# if older then a month | |
is_month=$(echo $line | grep 'month') | |
if [ ! -z "$is_month" ]; then | |
echo $id_img | |
docker rmi -f $id_img | |
continue | |
fi | |
# remove older then 4 weeks | |
num_week=$(echo $line | grep "week" | awk '{print $4}') | |
if [ ! -z "$num_week" ] && [ $num_week -ge $max_week_size ]; then | |
echo $id_img | |
docker rmi -f $id_img | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment