How can I delete Docker's images?

2019-01-07 01:39发布

I've the following images:

alex@alexvps:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              70c0e19168cf        5 days ago          1.069 GB
<none>              <none>              c2ce80b62174        8 days ago          399.2 MB
<none>              <none>              60afe4036d97        8 days ago          325.1 MB

and when I try to remove one of them I get:

alex@alexvps:~$ sudo docker rmi 60afe4036d97
Error: Conflict, 60afe4036d97 wasn't deleted
2014/01/28 00:54:00 Error: failed to remove one or more images

How can I remove them? Why is there such conflict?

标签: docker
16条回答
做个烂人
2楼-- · 2019-01-07 01:59

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 $_}
查看更多
迷人小祖宗
3楼-- · 2019-01-07 02:03

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)
查看更多
放我归山
4楼-- · 2019-01-07 02:04

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>
查看更多
ら.Afraid
5楼-- · 2019-01-07 02: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.

查看更多
Bombasti
6楼-- · 2019-01-07 02:06

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)
查看更多
The star\"
7楼-- · 2019-01-07 02:08

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

查看更多
登录 后发表回答