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 ?
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.
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> .
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.
As the comments on your question indicate, you have figured out that you need to use:
docker rmi
to remove imagesdocker rm
to remove containersfor a bit more background: there is a difference between:
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.
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>)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With