remove docker images containers volumes

How to Remove Docker Images, Containers & Volumes

Docker is a popular, open source container platform that allows you to easily build infrastructure independent applications. It is widely used by organizations and IT departments who find it easy to create, deploy and run applications. A container allows you to package an application along with its dependencies into an independent container that does not depend on operating system. It is a self-contained executable of an application that contains everything the application needs for running. It includes code, runtime, system tools & libraries, and even configurations. In this article, we will learn how to remove docker images, containers and volumes in Linux.


How to Remove Docker Images, Containers & Volumes

Here are the steps to remove docker images, containers & volumes. Before we proceed, here is how to list all docker images on your system.

$ docker image	        #list the most recently created images
OR
$ docker image -a 	#list all images

In the output below, you will see some of the images have TAG column as <none>. They are dangling images that are not related to any other image. They are basically not required anymore and are simply occupying disk space.


How to Remove Docker Images

You can easily remove docker images using docker rmi command followed by the image ID of the docker image to be removed, as shown below.

$ docker rmi image_id

Here are a couple of examples where we first remove only a single image, and then we remove multiple images.

$ docker rmi d65c4d6a3580 				#remove a single image
$ docker rmi 612866ff4869 e19e33310e49 abe0cd4b2ebc	#remove multiple images

If you want to only view dangling images directly, you can use the following command.

$ docker images -f dangling=true

Now if you want to remove all dangling images use either of the following commands.

$ docker image prune		#interactively remove dangling images
OR
$ docker rmi $(docker images -q -f dangling=true)

If you want to delete images that are not associated with any container, use the following command.

$ docker image prune -a 


How to Remove Docker Containers

First of all you can start by listing all docker containers in your system, using the following command.

$ docker ps
OR
$ docker ps -a  

The output will list information about all available containers.

Note the first column container ID of the container that you want to remove. Then use the following command to remove container.

$ docker rm container_id

Here is an example to remove container, single we as well as multiple containers.

$ docker rm 0fd99ee0cb61		#remove a single container
$ docker rm 0fd99ee0cb61 0fd99ee0cb61   #remove multiple containers

Please note, you cannot remove a container if it is running. In such cases, you need to stop the container first and then remove it.

$ docker stop 0fd99ee0cb61
$ docker rm -f 0fd99ee0cb61

If you want to remove a container that is running, you can force remove it with the following command. It will send a SIGKILL command to the container.

$ docker rm -f 0fd99ee0cb61

You can also remove containers based on their status. Here is an example to remove containers with status=exited.

$ docker rm $(docker ps -qa --filter "status=exited")

If you want to stop and remove all containers, run the following commands.

$ docker stop $(docker ps -a -q)  #stop all containers
$ docker container prune          #interactively remove all stopped containers
OR
$ docker rm $(docker ps -qa)


How to Remove Docker Volumes

Here is the command to list all docker volumes on your system.

$ docker volume ls

The above command will list all volumes on your system with their volume ID. Note the volume ID of volumes that you want to delete.

Here is the command to remove docker volume, single as well as multiple.

$ docker volume rm volume_ID 	           #remove a single volume 
$ docker volume rm volume_ID1 volume_ID2   #remove multiple volumes

If you want to force remove a certain volume ID, use the -f flag for this purpose.

$ docker volume rm -f volume_ID

To remove dangling volumes, use the following command.

$ docker volume rm $(docker volume ls  -q --filter dangling=true)

If you want to remove unused volumes, use the following command to interactively remove them.

$ docker volume prune	

Please note, you need to run the above commands with root or sudo privileges. If you want to run them without root or sudo privileges, you need to add this user to docker user group. Here is the command to add test_user to docker user group.

$ sudo usermod -a -G docker test_user

In this article, we have learnt how to remove docker images, containers and volumes.

Also read:

How to Rebuild RPM Database in RHEL/CentOS
How to Modify URL Without Reloading Page in JavaScript
How to List All Virtual Environments in Python
How to Transfer MySQL Database from One Computer to Another
How to List Installed PHP Modules in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *