How to remove old and unused Docker images

前端 未结 26 1829
北恋
北恋 2020-11-22 11:28

When running Docker for a long time, there are a lot of images in system. How can I remove all unused Docker images at once safety to free up the storage?

In additio

26条回答
  •  青春惊慌失措
    2020-11-22 12:03

    I'm using this command:

    export BEFORE_DATETIME=$(date --date='10 weeks ago' +"%Y-%m-%dT%H:%M:%S.%NZ")
    docker images -q | while read IMAGE_ID; do
        export IMAGE_CTIME=$(docker inspect --format='{{.Created}}' --type=image ${IMAGE_ID})
        if [[ "${BEFORE_DATETIME}" > "${IMAGE_CTIME}" ]]; then
            echo "Removing ${IMAGE_ID}, ${BEFORE_DATETIME} is earlier then ${IMAGE_CTIME}"
            docker rmi -f ${IMAGE_ID};
        fi;
    done
    

    This will remove all images whose creation time is greater than 10 weeks ago.

提交回复
热议问题