-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_old_img.sh
28 lines (22 loc) · 1.01 KB
/
remove_old_img.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
#author: Pawel Zarzycki.
#
#Script is removing images that are older than current one. As an argument it possible to specify how many of images are treated like "current".
#For example: there are images with tags 1.0.1 (oldest), 1.0.2, 1.0.3 (newest). If you pass argument 1 the script will remove 1.0.1 and 1.0.2 .
#If argument = 2 them the script will remove only 1.0.1 .
cli_args=("$@")
#How many images should be ignored as "current" ones.
cur_images=${cli_args[0]}
if [ -z "$cur_images" ]
then
cur_images=1
fi
# loop through stored images.
for img_id in $( docker images --format "{{.ID}}:{{.Repository}}" | grep -v REPOSITORY); do
image_rep=$(echo $img_id | cut -d":" -f2);
image_id=$(echo $img_id | cut -d":" -f1);
for img in $( docker images --format "{{.ID}}:{{.Repository}}:{{.Tag}}" --filter before=$(docker images | grep $image_id | awk '{print $3}' | uniq) | grep -w $image_rep | tail -n+$cur_images); do
# echo $img;
echo $(echo $img | cut -d":" -f1) | xargs -r docker rmi;
done;
done;