213

I am trying to delete a docker image 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
0

10 Answers 10

282

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.

5
  • 2
    Thanks, It worked fine but there is an issue. I deleted a 10 GB image but after deleting it, I cannot see any change in my free disk space. Commented Jul 6, 2018 at 9:36
  • 2
    did you remove volumes attach with image and did you check image size of each
    – Adiii
    Commented Jul 6, 2018 at 9:48
  • 2
    Not really, I am new to docker. I will try the tool.By the way can I use command line to detect volume and remove them? Commented Jul 6, 2018 at 11:22
  • 2
    yes docker volume ls ; to list volume and to remove, docker volume rm v_id
    – Adiii
    Commented Jul 7, 2018 at 0:23
  • 2
    To Understand the real issue behind, read the answer by angus
    – cryptonkid
    Commented Jul 22, 2021 at 7:49
88

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

4
  • 5
    This is part of any complete answer. You can either force removal or delete the stopped image. Commented Nov 7, 2018 at 17:47
  • 4
    how to list stopped containers? docker ps is empty
    – Gulzar
    Commented Nov 24, 2021 at 15:06
  • 11
    use docker ps -a
    – wubbalubba
    Commented Jan 14, 2022 at 16:50
  • 1
    you mean docker container ls -a ? Commented Feb 28, 2023 at 19:27
82

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>
2
56

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

#check container
docker ps -a

#remove container
docker rm containerID
0
19

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.

15

In case docker rmi <image-id> didn't work, try this:

Stop all running containers

docker stop $(docker ps -aq)

Remove all containers

docker rm $(docker ps -aq)

Remove all images

docker rmi $(docker images -q)
0
9

In addition to the other answers, I had the same issue using Docker for Mac but docker ps -a did not show any running containers. Running docker images shows:

docker/desktop-storage-provisioner   v1.0                605a0f683b7b        2 months ago        33.1MB
k8s.gcr.io/kube-controller-manager   v1.15.5             1399a72fa1a9        6 months ago        159MB
k8s.gcr.io/kube-proxy                v1.15.5             cbd7f21fec99        6 months ago        82.4MB
k8s.gcr.io/kube-apiserver            v1.15.5             e534b1952a0d        6 months ago        207MB
k8s.gcr.io/kube-scheduler            v1.15.5             fab2dded59dd        6 months ago        81.1MB
docker/kube-compose-controller       v0.4.23             a8c3d87a58e7        11 months ago       35.3MB
docker/kube-compose-api-server       v0.4.23             f3591b2cb223        11 months ago       49.9MB
k8s.gcr.io/coredns                   1.3.1               eb516548c180        16 months ago       40.3MB
k8s.gcr.io/etcd                      3.3.10              2c4adeb21b4f        17 months ago       258MB
k8s.gcr.io/pause                     3.1                 da86e6ba6ca1        2 years ago         742kB

All of these images are used by the Docker app if you have Kubernetes enabled. In the Docker for Mac preferences, there is a section for Kubernetes. If you click "Show system containers (advanced)", they will appear in docker ps.

If you wish to delete these images, you need to disable Kubernetes via the Docker for Mac app, then try again.

5

Docker containers run on top of docker images, you can delete an image only if it is not attached to any container.! So you have to stop container and remove container then you can remove image id.

docker stop container-id and docker rm container-id

Then try removing image docker rmi image-id

Some extra info given in below link. Cheers.!

https://docs.docker.com/config/pruning/

1
  • This should be the correct answer following the process of stop container (docker-compose stop )-> remove container (docker rm <name of the container>)-> remove associated images (docker rmi <image_id>).
    – kta
    Commented Mar 16, 2020 at 1:57
0

If you have changed in the Dockerfile and if you are using docker compose, you can just rebuild your image by run docker-compose with the --build option !

1
  • If you made any changes on the Dockerfile you shoudn't remove the image yourself just use the flag --build with docker-compose so it rebuild a new image with the changes
    – ans1genie
    Commented Apr 4, 2019 at 13:37
0

If you have created the docker image by passing --name, you can run the following to remove the container

Example of running docker container by name

/usr/bin/docker run --restart=always --name=dynamodb_docker -p=8000:8000 -v amazon/dynamodb-local

docker stop /dynamodb_docker
docker rm /dynamodb_docker

So, the correct format to remove a docker container is the following:

docker stop /<CONTAINER NAME>
docker rm /<CONTAINER NAME>

Not the answer you're looking for? Browse other questions tagged or ask your own question.