How can I delete Docker's images?

后端 未结 17 1105
执笔经年
执笔经年 2020-11-29 14:27

I\'ve the following images:

alex@alexvps:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
&         


        
相关标签:
17条回答
  • 2020-11-29 14:57

    The most compact version of a command to remove all untagged images is:

    docker rmi $(docker images | grep "^<none>" | awk '{print $"3"}')
    
    0 讨论(0)
  • 2020-11-29 14:57

    To delete some Docker image you must execute the following command:

    $ docker rmi <docker_image_id>
    

    So, to delete all Docker images you can execute the following command:

    $ docker rmi $(docker images -q)
    

    Now, if you want delete all Docker images (including images that are in use), you can add the flag -f, for example:

    $ docker rmi -f $(docker images -q)
    
    0 讨论(0)
  • 2020-11-29 14:58

    Remove all the containers

    docker ps -q -a | xargs docker rm
    

    Force remove all the Docker images

    docker rmi -f $(docker images -f dangling=true -q)
    
    0 讨论(0)
  • 2020-11-29 15:01

    The image could be currently used by a running container, so you first have to stop and remove the container(s).

    docker stop <container-name>
    docker rm <container-id>
    

    Then you could try deleting the image:

    docker rmi <image-id>
    

    You must be sure that this image doesn't depend on other images (otherwise you must delete them first).

    I had a strange case in which I had no more containers still alive (docker ps -a returned nothing) but I couldn't manage to delete the image and its image-dependency.

    To solve these special cases you could force the image removal with this:

    docker rmi -f <image-id>
    
    0 讨论(0)
  • 2020-11-29 15:01

    In Bash:

    for i in `sudo docker images|grep \<none\>|awk '{print $3}'`;do sudo docker rmi $i;done
    

    This will remove all images with name "<none>". I found those images redundant.

    0 讨论(0)
  • 2020-11-29 15:01

    If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the Docker image meltwater/docker-cleanup.

    That way you don't need to go clean it up by hand.

    Just run:

    docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw  -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
    

    It will run every 30 minutes (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.

    More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md

    0 讨论(0)
提交回复
热议问题