《原创》python定期清理docker中的无用镜像

import os
import sys
def main():
    member_pth = './member'
    txts = os.listdir(member_pth)
    member_images = []
    # get all image-ID that not deleted from all member
    print('********** start collect images-id of members **********')
    for i in txts:
        with open(os.path.join(member_pth, i), 'r') as f:
            while 1:
                txt_line = f.readline()
                if not txt_line:
                    break
                member_images.append(txt_line.strip('\n'))
    print(member_images)
    # get all image-ID in docker
    print('************* start collect images-id of system ***********')
    os.system('docker images -q > all-images.txt')
    all_images = []
    with open('all-images.txt', 'r') as af:
        while 1 :
            txt_a = af.readline()
            if not txt_a:
                break
            all_images.append(txt_a.strip('\n'))
    print(all_images)
    print('************** start delete images that not belong to member ***********')
    for i in all_images:
        if i not in member_images:
            os.system('docker rmi -f {}'.format(i))
            print('ID: {} is already deleted ......'.format(i))

if __name__ == '__main__':
    main()

 

posted @ 2022-06-02 12:51  海_纳百川  阅读(71)  评论(0编辑  收藏  举报
本站总访问量