Oddiy skript 4 haftadan eski barcha docker rasmlarni o'chirish uchun kerak

Men shunga o'xshash savollarni ilgari so'radim, lekin men kerakli narsani qiladigan hech narsa topa olmayapman. Men Docker Community forumlarida ham so'radim, lekin hech kim yordam berolmadi.

Yangi docker image budama buyrug'idan foydalana olmaysiz, chunki hozirgi versiya 1.11.2 dan eng so'nggi pog'ona 1,13 ga ko'tarolmasligim mumkin, ammo menda oddiy skript kerak. 1 oydan ziyod vaqtdan beri barcha rasmlarni silkitib tashlash yoki yo'q qilish. Konteynerlar haqida qayg'urishga hojat yo'q, chunki bu faqat tasvirlarni saqlaydigan repo-dan.

Agar docker tasvirlari "4 xafta" ni "1 oy" deb hisoblasa, oddiy bo'lishi mumkin, men shunga o'xshash biror narsani ishlatishim mumkin

docker images | grep " [months]* "

va docker rmi buyrug'iga chiqing. Biroq, "haftalar" da 11 haftagacha va faqat 3 oylik yoki undan ko'p vaqt o'tgachgina "oylar" haqida xabar bergani uchun buni qila olmayman.

docker images | grep " [months|weeks]* ago" | awk '{print $3,$4,$5}'

menga bu mahsulotni beradi:

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?

2
Men docker $ (docker images -q) ni tekshirishni istayman va kerakli maydonni toping
qo'shib qo'ydi muallif user2915097, manba

6 javoblar

Faqat awk kerak:

docker images |awk '$5=="months" || ($4>4 && $5=="weeks") {print $3,$4,$5}'

Sharh

Agar 5-maydon "oy" bo'lsa yoki "haftalar" ni va 4dan katta 4 bo'lsa, kerakli ustunlarni chop eting.

3
qo'shib qo'ydi

Bu erda bir buyruq fayli mavjud va siz uni .bashrc da yozishingiz mumkin

#!/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
2
qo'shib qo'ydi

Men shaxsan quyidagi bash funksiyasini afzal ko'raman:

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[@]}
}

Buni .bashrc tagiga qo'yib, uni tozalashni istagan har bir narsani docker_housekeeping deyishingiz mumkin.

1
qo'shib qo'ydi

Buni harakat qilib ko'ring (faqat awk dan foydalanish):

docker images |awk '{if (($4 > 4 && $5 == "weeks") || ($4 > 1 && $5 == "months")) {print $3,$4,$5}}'

Im foydalanish if i ifodasini va bajarish tartibini ta'minlash uchun Qavslar.

Siz kerak emas uchun grep .

1
qo'shib qo'ydi
Tahrirlangan javobni ko'ring.
qo'shib qo'ydi muallif Farhad Farahi, manba
Farxodga minnatdorman, ammo u ishlamadi - 10 xafta qolgan tasvirni sog'inmoqda.
qo'shib qo'ydi muallif HelenH, manba
Bu hozirda ishlaydi, rahmat
qo'shib qo'ydi muallif HelenH, manba

Siz quyida ko'rsatilgan kodni sinab ko'rishingiz mumkin

#!/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

Endi bu bo'shliq ma'lumotlarini yozish bilan tozalaydi

1
qo'shib qo'ydi

Filtrni kiritish uchun -f bayrog'idan foydalanishingiz mumkin.

4 yoshdan kattaroq rasm identifikatorini toping. Va quyidagi buyruqni bajaring

docker rmi $(docker images -q -f "before=")

0
qo'shib qo'ydi