Reputation: 55
I know similar questions have been asked before, but I cannot find anything that does exactly what I need. I've also asked on the Docker Community forums, but nobody could help.
I don't have access to the new docker image prune
command, as I cannot upgrade from my current version 1.11.2 to the latest whizzy 1.13 yet, but I need a simple script that can be run under cron to remove all images, whether dangling or not, over 1 month old. I don't need to worry about containers, as this is from a repo that only stores images.
If docker images reported "4 weeks" as "1 month" it would be simple, I could use something like
docker images | grep " [months]* "
and pipe the output to the docker rmi
command. However, since it appears to report in "weeks" up to 11 weeks, and only "months" once the image is 3 months old or over, I can't do this.
docker images | grep " [months|weeks]* ago" | awk '{print $3,$4,$5}'
will give me this output::
6828f152f9cc 2 weeks
52a7412befd3 2 weeks
04c2b29e5e08 2 weeks
cdfb37d22663 2 weeks
ca38a8cabb2b 3 weeks
8b043f2395ba 3 weeks
2aa8b20380a0 3 weeks
1ac7dcf35935 9 weeks
f80873c4845b 9 weeks
d5700d37ee95 9 weeks
594dc21de8de 9 weeks
0c9dacb7fa7c 10 weeks
11f3eec60e17 3 months
b13ebf1a4999 3 months
68b0d6a3be3b 7 months
baadc9c8b0ce 8 months
10eb0d6b2b99 9 months
... but I'm stuck trying to figure out how to test $4,$5 to only include anything that's either "months" or >= "4 weeks" which can then be piped through to docker rmi . Anyone any clues for a simple bit of code that can do this, please?
Upvotes: 3
Views: 4951
Reputation: 169
You can try out below script, i am using that below
#!/bin/bash
timestamp=$(date +%Y%m%d_%H%M%S)
log_path="`pwd`"
filename=docker_cleanup_$timestamp.log
log=$log_path/$filename
docker_space_before(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space:" >> $log
echo $CURRENTSPACE >>$log
}
docker_find (){
echo "#####################################################################" >> $log
echo "Finding images" >> $log
echo "#####################################################################" >> $log
REMOVEIMAGES=`docker images | grep " [days|months|weeks]* ago" | awk '{print $3}'`
echo "Listing images that needs to be cleaned up" >> $log
echo $REMOVEIMAGES >>$log
}
docker_cleanup(){
echo "#####################################################################" >> $log
echo "Cleaning images" >> $log
echo "#####################################################################" >> $log
docker rmi ${REMOVEIMAGES}
}
docker_space_after(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space, after clean up:" >> $log
echo $CURRENTSPACE >>$log
}
docker_space_before
docker_find
docker_cleanup
docker_space_after
now this will clean up with logging the space information
Upvotes: 1
Reputation: 199
You can use the -f
flag to add a filter.
Find the image id which is older than 4 weeks old. And execute the following command
docker rmi $(docker images -q -f "before=<docker_image_id_older_than_4_weeks>")
Upvotes: 1
Reputation: 39277
Try this (Using awk
only):
docker images |awk '{if (($4 > 4 && $5 == "weeks") || ($4 > 1 && $5 == "months")) {print $3,$4,$5}}'
Im using if statement, and parenthesis to ensure order of execution.
You don't need to grep
.
Upvotes: 1
Reputation: 14955
Just awk
needed:
docker images |awk '$5=="months" || ($4>4 && $5=="weeks") {print $3,$4,$5}'
Explanation
If 5th field is "months", or equals to "weeks" and 4th greater than 4, print the required columns.
Upvotes: 4
Reputation: 12140
I personally prefer the following bash function:
docker_housekeeping() {
# Collect list of images older than 4 weeks.
OLD_IMAGES=$((docker images |
grep -P "(months|[4-9] weeks|\d\d+ weeks)" |
awk '{print $3}'))
# Remove old images.
docker rmi ${OLD_IMAGES[@]}
}
You can stick it your .bashrc
and just call it like docker_housekeeping
everytime you want to do clean up.
Upvotes: 1
Reputation: 1353
here is a script, and you can write it in your .bashrc
#!/bin/bash
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
Upvotes: 3