How to Remove Docker Images and Containers

Channel: Linux
Abstract: Docker provides rm option. Using this we can delete any docker containers from our local system. For example use below command with changing

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.

  • How to Export and Import Docker Containers
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)

Ref From: tecadmin

Related articles