How to remove old and unused Docker images

前端 未结 26 1345
北恋
北恋 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.

    0 讨论(0)
  • 2020-11-22 12:06

    How to remove a tagged image

    1. docker rmi the tag first

    2. docker rmi the image.

      # that can be done in one docker rmi call e.g.: # docker rmi <repo:tag> <imageid>

    (this works Nov 2016, Docker version 1.12.2)

    e.g.

    $ docker images 
    REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
    usrxx/the-application   16112805            011fd5bf45a2        12 hours ago        5.753 GB
    usryy/the-application   vx.xx.xx            5af809583b9c        3 days ago          5.743 GB
    usrzz/the-application   vx.xx.xx            eef00ce9b81f        10 days ago         5.747 GB
    usrAA/the-application   vx.xx.xx            422ba91c71bb        3 weeks ago         5.722 GB
    usrBB/the-application   v1.00.18            a877aec95006        3 months ago        5.589 GB
    
    $ docker rmi usrxx/the-application:16112805 && docker rmi 011fd5bf45a2
    $ docker rmi usryy/the-application:vx.xx.xx && docker rmi 5af809583b9c
    $ docker rmi usrzz/the-application:vx.xx.xx eef00ce9b81f
    $ docker rmi usrAA/the-application:vx.xx.xx 422ba91c71bb
    $ docker rmi usrBB/the-application:v1.00.18 a877aec95006
    

    e.g. Scripted remove anything older than 2 weeks.

    IMAGESINFO=$(docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}} {{.CreatedSince}}' |grep -E " (weeks|months|years)")
    TAGS=$(echo "$IMAGESINFO" | awk '{ print $2 ":" $3 }' )
    IDS=$(echo "$IMAGESINFO" | awk '{ print $1 }' )
    echo remove old images TAGS=$TAGS IDS=$IDS
    for t in $TAGS; do docker rmi $t; done
    for i in $IDS; do docker rmi $i; done
    
    0 讨论(0)
  • 2020-11-22 12:07

    Assuming you have Docker 1.13 or higher you can just use the prune commands. For your question specifically for removing old images, you want the first one.

    # Remove unused images
    docker image prune
    
    # Remove stopped containers.
    docker container prune
    
    # Remove unused volumes
    docker volume prune
    
    # Remove unused networks
    docker network prune
    
    # Command to run all prunes:
    docker system prune
    

    I would recommend not getting used to using the docker system prune command. I reckon users will accidentally remove things they don't mean to. Personally, I'm going to mainly be using the docker image prune and docker container prune commands.

    0 讨论(0)
  • 2020-11-22 12:08

    @VonC already gave a very nice answer, but for completeness here is a little script I have been using---and which also nukes any errand Docker processes should you have some:

    #!/bin/bash
    
    imgs=$(docker images | awk '/<none>/ { print $3 }')
    if [ "${imgs}" != "" ]; then
       echo docker rmi ${imgs}
       docker rmi ${imgs}
    else
       echo "No images to remove"
    fi
    
    procs=$(docker ps -a -q --no-trunc)
    if [ "${procs}" != "" ]; then
       echo docker rm ${procs}
       docker rm ${procs}
    else
       echo "No processes to purge"
    fi
    
    0 讨论(0)
  • 2020-11-22 12:09

    This worked for me:

    docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
    
    0 讨论(0)
  • 2020-11-22 12:09

    If you want to remove images pulled X months ago, you can try the below example which remove images created three months ago:

    three_months_old_images=`docker images | grep -vi "<none>" | tr -s ' ' | cut -d" " -f3,4,5,6 | grep "3 months ago" | cut -d" " -f1`
    docker rmi $three_months_old_images
    
    0 讨论(0)
提交回复
热议问题