image is being used by stopped container

I am trying to delete a docker container by this command:

docker rmi <Image-Id>

Obviously, I have replaced the Image-Id by the Id I get using:

docker images

But I see the error below:

Error response from daemon: conflict: unable to delete <Image-ID> (must be forced) - image is being used by stopped container xxxxxxxxxxx

You can also use --force , -f Force removal of the image

If you use the -f flag and specify the image’s short or long ID, then this command untags and removes all images that match the specified ID.

  docker rmi -f <image_id> 

Note: this command removes images being used by containers.


You need to delete the stopped container with docker rm, and then you can delete the image it uses with docker rmi.


You may also find that you have stopped containers that are causing the lock. Remove these first using:

docker rm  $(docker ps -q -a)

Here we are listing the docker processes by just the ID and then removing those; however, docker rm will only remove stopped containers.

Next go back and remove the images using:

docker image rm <image_id>

You must remove container first.
Forced is the last method to take.

#check container
docker ps -a

#remove container
docker rm containerID

If the goal is to reclaim disk space, another approach not yet mentioned is to use docker system prune. It will print a warning which should be heeded:

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

This will not delete named images, but will purge intermediate unnamed images. Depending on the sizes of containers you're using, this can save a considerable amount of disk space.