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 find all images on your system use 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
If you want to remove all docker containers. You can use simply following commands. The first command will stop all running Docker containers and the second command will delete them.
Stop All Containers
docker stop $(docker ps -a -q)
Remove All Containers
docker rm $(docker ps -a -q)