Delete Docker Image

Sometimes, when you run a docker image and then try to delete it, you might get an error message similar to following.

Error response from daemon: conflict: unable to delete 2867b9f04038 (must be forced) - image is being used by stopped container caff7e1e3508
Delete Docker Image

We shall present you a step-by-step guide to delete a docker image properly.

ADVERTISEMENT

1. Stop the container

You can get the container id from the error message. “image is being used by stopped containercaff7e1e3508

Following is the syntax to stop the container.

$ docker stop <containerid>

Example

root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker stop caff7e1e3508
caff7e1e3508

The container is stopped and echoes back with the container id.

2. Remove the container

Once the container is stopped, we have to remove the container.

Following is the syntax to remove the container.

$ docker rm <containerid>

Example

root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker rm caff7e1e3508
caff7e1e3508

3. Remove Docker Image

Once the container is removed, we are good to go with removing Docker Image.

Following is the syntax to remove the image.

$ docker rmi <imageid>

Example

root@arjun-VPCEH26EN:/home/arjun/workspace/docker# docker rmi 2867b9f04038
Untagged: java-application:latest
Deleted: sha256:2867b9f04038b6a8e4ffdad800d4ea9ad7fa8ba1ea5fd4e967e7731ef615199d
Deleted: sha256:d858962818586481bb955bb5791e775e37411b0f9e870fe9826858928c53410a
Deleted: sha256:a81438c31d4f1ec303b4872cabe239907e8e8cecf6787505bb28085c20b25f82
root@arjun-VPCEH26EN:/home/arjun/workspace/docker#

The docker image is removed successfully.

Conclusion

In this Docker Tutorial – Delete Docker Image, we have successfully removed the image from Docker’s images.