본문 바로가기
IT/기타

Docker 특정 image 주기 삭제 (스크립트 포함)

by JGSHIN 2025. 2. 18.
SMALL

이미지 CI/CD 시 새로운 태그 버전을 도커 서버에 설치 후 최신 버전이 올라가면 이전 버전 이미지들이 남는 현상때문에 만들게 되었습니다..

 

#!/bin/bash

current_date=$(date +%s)

docker images | while read -r line

do

    columns=($line)

    repository=${columns[0]}

    created=${columns[4]}

 

    if [[ $repository == *"dev-jgshin.co.kr"* ]] && [[ $created == *"months"* ]]

    then

        months_ago=${created%" months ago"}

        difference=$((current_date - (months_ago * 30 * 24 * 60 * 60)))

 

        if [[ $difference -ge 2592000 ]]

        then

            image_id=${columns[2]}

            echo "Force deleting image $repository:$image_id"

            docker rmi -f $image_id

        fi

    fi

done

 

빨간 영역이 해당 이미지 이름 부분만 바꿔서 스케줄러에 등록후 사용 하시면 됩니다.

LIST
📧 이메일 문의
by @ 2025 JGSHIN