[docker] How can I delete Docker's images?

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?

This question is related to docker

The answer is


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.


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 most compact version of a command to remove all untagged images is:

docker rmi $(docker images | grep "^<none>" | awk '{print $"3"}')

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.


Use

docker image prune -all

or

docker image prune -a

Remove all dangling images. If -a is specified, it will also remove all images not referenced by any container.

Note: You are prompted for confirmation before the prune removes anything, but you are not shown a list of what will potentially be removed. In addition, docker image ls does not support negative filtering, so it difficult to predict what images will actually be removed.

As stated under Docker's documentation for prune.


I found the answer in this command:

docker images --no-trunc | grep none | awk '{print $3}' | xargs docker rmi

I had your problem when I deleted some images that were being used, and I didn't realise (using docker ps -a).


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


First, remove all the containers using the following command

sudo docker ps -a -q | xargs -n 1 -I {} sudo docker rm {}

Then, remove the image by its ID using the following command

sudo docker rmi <image-id>

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>

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.


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.


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 $_}

I have found a solution with Powershell script that will do it for me.

The script at first stop all containers than remove all containers and then remove images that are named by the user.

Look here http://www.devcode4.com/article/powershell-remove-docker-containers-and-images


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`

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

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)