How to remove old and unused Docker images

前端 未结 26 1397
北恋
北恋 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:26

    Here is a script to clean up Docker images and reclaim the space.

    #!/bin/bash -x
    ## Removing stopped container
    docker ps -a | grep Exited | awk '{print $1}' | xargs docker rm
    
    ## If you do not want to remove all container you can have filter for days and weeks old like below
    #docker ps -a | grep Exited | grep "days ago" | awk '{print $1}' | xargs docker rm
    #docker ps -a | grep Exited | grep "weeks ago" | awk '{print $1}' | xargs docker rm
    
    ## Removing Dangling images
    ## There are the layers images which are being created during building a Docker image. This is a great way to recover the spaces used by old and unused layers.
    
    docker rmi $(docker images -f "dangling=true" -q)
    
    ## Removing images of perticular pattern For example
    ## Here I am removing images which has a SNAPSHOT with it.
    
    docker rmi $(docker images | grep SNAPSHOT | awk '{print $3}')
    
    ## Removing weeks old images
    
    docker images | grep "weeks ago" | awk '{print $3}' | xargs docker rmi
    
    ## Similarly you can remove days, months old images too.
    

    Original script

    https://github.com/vishalvsh1/docker-image-cleanup

    Usually Docker keeps all temporary files related to image building and layers at

    /var/lib/docker

    This path is local to the system, usually at THE root partition, "/".

    You can mount a bigger disk space and move the content of /var/lib/docker to the new mount location and make a symbolic link.

    This way, even if Docker images occupy space, it will not affect your system as it will be using some other mount location.

    Original post: Manage Docker images on local disk

提交回复
热议问题