Python/2.7 simple coding(+ c++)

remove docker image over 2 month

qkqhxla1 2020. 3. 4. 13:46

로컬에서 테스트로 도커 빌드를 자주 해보는데 이미지가 계속 쌓인다. 이미지를 계속해서 만들다보면 도커의 이미지를 저장하는곳의 공간이 풀나서 정말 몇개월에 한번씩 이미지를 더이상 생성할수 없다고 나오는데 2달이 지난 이미지는 필요없다고 판단해 지우는 스크립트를 하나 만들었다. 

 

여기에 저장해놓고 가져다가 쓰려고 간단하게 만들어둠. python3

import subprocess
import re

def get_docker_image_list(name):
    docker_image_list, _ = subprocess.Popen(['docker', 'image', 'ls'],
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE).communicate()
    docker_image_list = docker_image_list.decode('utf-8')
    docker_image_list = filter(lambda x: name in x, docker_image_list.split('\n')[:-1])
    return list(map(lambda x: re.split('\s{2,}', x), docker_image_list))


def remove_image_over_2_month(image_list):
    month_regex = re.compile('(\d+)\s+months\s+ago')
    year_regex = re.compile('(\d+)\s+years\s+ago')
    for image in image_list:
        repository, tag, image_id, created, size = image
        month = re.search(month_regex, created)
        year = re.search(year_regex, created)  # 1년이상된이미지 다 지움
        if month and int(month.group(1)) > 2 or year:
            stdout, _ = subprocess.Popen(['docker', 'image', 'rm', '-f', image_id],
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE).communicate()
            print(stdout)


if __name__ == '__main__':
    image_name = ''
    image_list = get_docker_image_list(image_name)
    remove_image_over_2_month(image_list)

 

'Python > 2.7 simple coding(+ c++)' 카테고리의 다른 글

파일 입출력.  (0) 2016.10.04
vector, hashmap,  (0) 2016.09.29
string, regular expression관련.  (0) 2016.09.29
canyouhack.it Programming Challenge 3 Lost!  (0) 2015.05.03
canyouhack.it Programming Challenge 2 Sudoku!  (0) 2015.05.01