Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker rmi unable to remove images

Tags:

docker

So , I have a bunch of untagged images on my host. I use

sudo docker rmi $(sudo docker images | grep "<none>" | awk '{print($3)}') to delete these images. On execution , I get error

Error response from daemon: Conflict, cannot delete 31fa814ba25a because the container 70c20aa2c19f is using it, use -f to force

So I do a

sudo docker rmi 70c20aa2c19f

on which I get the error

Error response from daemon: No such image: 70c20aa2c19f

So if there's no image with ImageID 70c20aa2c19f , then why the initial delete command's error states that there is an image with ImageID 70c20aa2c19f ?

like image 476
amrx Avatar asked May 05 '15 08:05

amrx


People also ask

Can not delete docker images?

In short, Docker error unable to delete image must be forced occur if some containers are running on it. If it is a stopped container we forcefully remove it. If not we stop and remove the container and delete the image.

How do I remove an image from docker repository?

To remove the image, you first need to list all the images to get the Image IDs, Image name and other details. By running simple command docker images -a or docker images . After that you make sure which image want to remove, to do that executing this simple command docker rmi <your-image-id> .

How do I remove docker image referenced in multiple repositories?

You cannot remove images having multiple repositories without the force modifier, see Docker docs for more info. If you use the -f flag and specify the image's short or long ID, then rmi untags and removes all images that match the specified ID.


2 Answers

As the comments on your question indicate, you have figured out that you need to use:

  • docker rmi to remove images
  • docker rm to remove containers

for a bit more background: there is a difference between:

  1. A docker image,
  2. A running container based off that image and
  3. A stopped container based off that image

The stopped container is kept because running the container might have changed the file system in the container, you can then commit this stopped container to have a new image. (that's one way to create images, manually run the commands and commit the resulting container).

Creating images using docker build and Dockerfile, does the same thing, it runs the container executing the Dockerfile commands and commits the resulting images, only tagging the last image that was committed.

like image 81
Vincent De Smet Avatar answered Sep 19 '22 23:09

Vincent De Smet


The command below worked great for me, just keep adding the grep -v's for all the containers you want to keep:

sudo docker rm -f $(sudo docker ps -a -q | grep -v <good_container_1> | grep -v <good_container_2>)
like image 32
stefletcher Avatar answered Sep 17 '22 23:09

stefletcher