How To Remove Docker Images, Containers, and Volumes

How To Remove Docker Images, Containers, and Volumes. Docker simplifies building and deployment of containers. It packages them inside containers that run on any computing platform. Containers are isolated and standalone environments, containing all the prerequisites needed to run an application including libraries, dependencies, configuration files and source code. Enhances the shipping and testing code and significantly cuts down on delays between building applications and deploying them in production..

Over time, your system is cluttered with unused images, containers, and data volumes. These easily fill up your disk space. Thankfully, docker provides a set of commands and tools to declutter your system and gets rid of such idle resources. In this guide, we demonstrate how to remove docker images, containers and volumes.

Prerequisites

Linux system with Docker installed. In this guide we use Ubuntu 22.04 as our test environment.

Let’s see how you create and remove unused resources by way of Docker images, containers and volumes.

How to Remove Docker Images

There are various approaches to removing docker images. Remove a single, multiple, or all images on the command line. 

Remove a Single Docker Image

The docker rmi command is used to remove a docker image using the Image ID as shown in the following syntax:

				
					docker rmi IMAGE_ID
				
			

To demonstrate this, we pull the latest Redis image from the Docker hub as follows:

				
					docker pull redis:latest
				
			

To list all the images present on your system, run the command.

				
					docker images -a
				
			

This prints out the following details about the Docker image:

Take note of the Image ID, as this is used when deleting the image.

To delete the Redis image, run the following command. Of course, replace the Image ID to correspond with what you have in your setup.

				
					docker rmi 33e3db53b328
				
			

Once the removal process is complete, confirm that the image no longer exists as shown.

				
					docker images -a
				
			

Since the image has already been removed, this time around, the image is listed.

Remove multiple Docker Images

Remove multiple images in single command by listing the ID of the image separated by a space as shown.

				
					docker rmi IMAGE_ID_1 IMAGE_ID_2 IMAGE_id_3
				
			

In this example we are deleting Alpine and Nginx images in one command.

				
					docker rmi 6efc10a0510f 9ed4aefc74f6
				
			

Remove all Docker Images

To delete or remove all the docker images on your system, run the following command:

				
					docker rmi $(docker images -a -q)

				
			

How to Remove Docker Containers

In this section, we explore various ways of removing Docker containers.

Removing a Single Container

Before removing any containers, you list them using the docker ps command as shown. The -a argument displays all the containers, both actively running and exited.

				
					docker ps -a
				
			

The output shows 2 containers. The first one is the ‘hello-world’ container, which has already exited and the second is an Apache web server container which is still running.

You can remove a container using its Container ID or Name, found on the last column labelled ‘NAMES‘.

				
					docker rm container_ID
				
			

OR

				
					docker rm container_NAME
				
			

 When the container is successfully removed, its ID is displayed.

For example, To remove the ‘Hello-world’ container using the Container ID, we run the command:

				
					docker rm 5e13a197c007
				
			

We verify that the container has been removed by listing all the containers.

				
					docker ps -a
				
			

This time around, only one container is listed in the output.

NOTE

To remove a running container, you must stop the container using the docker stop command. Otherwise, you encounter an error.

Let’s now remove the remaining container using its container name as shown.

				
					docker rm eloquent_lamarr
				
			

We have encountered an error and the output gives the reason why.  Well, Docker cannot allow us to remove a running container unless it is stopped first. Therefore we are going to stop the running container as shown.

				
					docker stop eloquent_lamarr
				
			

The output displays the container name. This is a confirmation that the command run successfully and that the container was stopped. Now we proceed to remove the container.

				
					docker rm eloquent_lamarr
				
			

Removing containers once they have exited

Some containers still remain on your system once they are created and exited. For example, when you run docker run hello-world, Docker daemon downloads the hello-world image from Docker hub and creates a container from the image. The container then streams output on the terminal and then exits .The docker ps -a command reveal the existence of the container in an exited status.

				
					docker ps -a
				
			

From the output, you see the container as an ‘Exited‘ status. List exited containers only as shown.

				
					docker ps -a -f status=exited
				
			

To remove all ‘Exited‘ containers, run the command

				
					docker rm $(docker ps -a -f status=exited -q)

				
			

Delete all Docker Containers

At times, you might want to delete all the containers, whether running or exited. The first approach is to review all the containers first, regardless of their status:

				
					docker ps -a
				
			

Next, stop all the running containers:

				
					docker stop $(docker ps -a -q)
				
			

Finally, delete all the containers:

				
					docker rm $(docker ps -a -q)
				
			

How to Remove Docker Volumes

Data generated by containers is ephemeral and ceases to exist once the container is removed. This is where data volumes come in. Docker volumes are file systems that persist data outside the container. They preserve data generated by containers for backup purposes.

To list existing data volumes, run the command:

				
					docker volume ls
				
			

To remove a docker volume:

				
					docker volume rm volume_name
				
			

If the volume is still in use, you get an error as shown below:

To address the issue, you need to first stop and remove the container. If you are using Docker compose, run the following command:

				
					docker-compose down
				
			

The above command stops and removes the container. From here , you remove it, as shown.

				
					docker volume rm root_my-datavolume
				
			

How to remove dangling volumes

Generally, when a container is removed, the volume is not automatically deleted or removed. The volume persists, however, it is not connected to any container. When this happens, the volume is said to be a dangling volume (undesired as it is an unused resource and serves no purpose).

To  list dangling volumes by filtering out the docker volume ls command as follows.

				
					docker volume ls -f dangling=true
				
			

To remove the dangling volume:

				
					docker volume prune
				
			

How to remove a container alongside its volume

A volume is deleted simultaneously with its container using the -v flag. However, this only applies to unnamed volumes. When the container is successfully removed, its ID is displayed. However, no reference is made to the volume removal. It is silently purged from the system.

To remove a container and its volume:

				
					docker rm -v container_name
				
			

Removing all Unused Docker Objects

Remove all stopped containers, dangling images and unused networks using the following command.

				
					docker system prune
				
			

This prompts you to proceed. Simply press ‘Y’ and hit ENTER.

The same command is to be run using the -f (–force) flag to bypass the prompt.

Thank you for reading article How To Remove Docker Images, Containers, and Volumes. We shall conclude this article now. 

How To Remove Docker Images, Containers, and Volumes Conclusion

In this tutorial, we have demonstrated how to remove docker images, containers, and volumes. As you have seen, there are various commands for removing each resource along with various command-line options or flags. Check out the Docker documentation for more information on how to do it. 

Avatar for James Kiarie
James Kiarie

Hello everyone! My name is James, a certified Linux Administrator, and a tech enthusiast with over 5 years of experience in penning down high-quality guides on Linux and Cloud technologies. Outside work hours I enjoy working out, swimming, listening to music, and reading fiction novels.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x