编程语言
首页 > 编程语言> > 《原创》python定期清理docker中的无用镜像

《原创》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()

 

标签:__,python,member,print,images,镜像,docker,txt,os
来源: https://www.cnblogs.com/chentiao/p/16337404.html