How can I delete Docker's images?

后端 未结 17 1108
执笔经年
执笔经年 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 15:03

    Possible reason: The reason can be that this image is currently used by a running container. In such case, you can list running containers, stop the relevant container and then remove the image:

    docker ps
    docker stop <containerid>
    docker rm <containerid>
    docker rmi <imageid>
    

    If you cannnot find container by docker ps, you can use this to list all already exited containers and remove them.

    docker ps -a | grep 60afe4036d97
    docker rm <containerid>
    

    Note: Be careful of deleting all exited containers at once in case you use Volume-Only containers. These stay in Exit state, but contains useful data.

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

    Simply you can aadd --force at the end of the command. Like:

    sudo docker rmi <docker_image_id> --force
    

    To make it more intelligent you can add as:

    sudo docker stop $(docker ps | grep <your_container_name> | awk '{print $1}')
    
    sudo docker rm $(docker ps | grep <your_container_name> | awk '{print $1}')
    
    sudo docker rmi $(docker images | grep <your_image_name> | awk '{print $3}') --force
    

    Here in docker ps $1 is the first column, i.e. the Docker container ID.

    And docker images $3 is the third column, i.e. the Docker image ID.

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

    Since Docker ver. 1.13.0 (January 2017) there's the system prune command:

    $ docker system prune --help
    
    Usage:  docker system prune [OPTIONS]
    
    Remove unused data
    
    Options:
    -a, --all     Remove all unused images not just dangling ones
    -f, --force   Do not prompt for confirmation
        --help    Print usage
    
    0 讨论(0)
  • 2020-11-29 15:09

    You have to stop/delete all unnecessary containers created on that images first.

    Have a look: How to remove old Docker containers.

    After that use @marcell solution.

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

    The reason for the error is that even though the image did not have any tag, there still exists a container created on that image which might be in the exited state. So you need to ensure that you have stopped and deleted all containers created on those images. The following command helps you in removing all containers that are not running:

    docker rm `docker ps -aq --no-trunc --filter "status=exited"`
    

    Now this removes all the dangling non-intermediate <none> images:

    docker rmi `docker images --filter 'dangling=true' -q --no-trunc`
    

    Note: To stops all running containers:

    docker stop `docker ps -q`
    
    0 讨论(0)
  • 2020-11-29 15:12

    In addition to Sunny's answer:

    In order to delete all images on a Windows machine (with Docker for Windows) in a PowerShell window, do:

    docker images -q | %{docker rmi -f $_}
    

    In order to delete all containers on a Windows machine (with Docker for Windows) in a PowerShell window, do:

    docker ps -a -q | %{docker rm -f $_}
    
    0 讨论(0)
提交回复
热议问题