In our previous tutorials you have learned about installation of Docker engine on CentOS/RHEL and Ubuntu operating system and pulled images from Docker hub. After that created containers with images. This tutorial will help you to remove unnecessary Docker images and containers from your host machine.
Remove Docker Images
To remove an images, Docker provides rmi
option. Using this we can delete any docker images from our local system. For example use below command with changing <IMAGE ID>
with your Docker image id.
docker rmi <IMAGE ID>
To list all available docker images on your system use the following command. This will help you to find the ID of images.
docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos latest 2933d50b9f77 11 days ago 196.6 MB ubuntu latest 36248ae4a9ac 11 days ago 188 MB
Remove Docker Containers
To remove a containers, Docker provides rm
option. Using this we can delete any docker containers from our local system. For example use below command with changing <CONTAINER ID>
with your Docker container id.
docker rm <CONTAINER ID>
To list all containers on your system using ps option, but ps will show only running containers. So to view all containers use -a parameter with ps.
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f2582758af13 ubuntu "/bin/bash" 2 hours ago Up 2 hours first_ubuntu 2b199b9976c4 centos "/bin/bash" 3 days ago Up 3 hours thirsty_yalow
Stop & Remove All Docker Containers
To remove all docker containers from your system use the following commands. The first command will stop all running Docker containers and then the second command will delete all docker containers from your system.
Stop All Containers
docker stop $(docker ps -a -q)
Remove All Containers
docker rm $(docker ps -a -q)
9 Comments
Thanks Rahul.
Thank you bro .. this work :v
Awesome Article and helpful to fix my Docker container force remove, can you also let me know who to remove attached volume?
dhanyawaad Rahul!
What should be the deletion cycle?
container 1st, then images 2nd and volume 3rd? Or something else you would like to suggest?
That was points of fact! In this article you hold an exciting instructive information and utilized for my exploration.
Excellent article. Exceptionally fascinating to read. I truly love to read such a pleasant article. Thanks! keep rocking.
Thank you very much for sharing lines of code that will help me to get best things. I will try this code myself to see the results.
if we create a docker with a name following steps mentioned by you still leaves instance, and if you try to create a container of an image with the same name you will get error. that container already exists even if you have stopped and removed the container.