Docker容器

《原创》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定期清理docker中的无用镜像的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!