How to Remove Docker Images, Containers, and Volumes

Published on February 9, 2024

Docker is one way of wrapping applications and services as part of your containerization option to help run applications anywhere. When working with docker, you will create images, containers, and volumes. When you no longer need some or all of the images, containers, and volumes, you can clean docker by removing them.

Removing unused containers or images will help keep a clean space, and this post has shared the steps and commands to remove docker images, containers, and volumes without messing up. Why don’t you stick around and let’s go through the whole process together?

Easy Steps on Removing Docker Images, Containers, and Volumes

It’s not one instance that you will find yourself with dangling images, containers, and volumes. It could be that some of what you created earlier is no longer needed, hence the need to remove them. Or maybe the purpose of a given image, container, or volume has been deprecated, and you want to have a clean space before deploying your application. Whatever the case, we have a guide on removing docker images, containers, and volumes. We’ve categorized them as shown below.

1) How to Remove Docker Containers

Before removing a container, the first step is to view all the containers in your docker. To display all containers regardless of their state, run the below command.

Copy

The output you will get will show the container names and IDs. Moreover, it will display the images under which the containers are created and their status.

Instead of showing all the containers, you can combine the above command with the grep keyword to filter the output. Once you have the container you want to remove, stop its state using the syntax below.

Copy

Note that you can refer to the container using its name or id, as shown in the image below.

To remove the stopped docker container, use the syntax below. Similarly, reference the container using its name or id.

Copy

In the above, we’ve tried listing the removed container and verified that it no longer appears, confirming that we successfully removed it.

Instead of removing a single container, you may choose to remove several based on a criteria. For instance, if you want to find all the exited docker containers and remove them, you would use the syntax below.

Copy

The above command first lists the exited docker containers to identify them before it removes them.

Run the two commands below if you prefer removing all the containers to get a clean workspace. The first command stops all the containers, while the second removes them.

Copy
Copy

2) How to Remove Docker Volumes

You can remove one or more docker volumes depending on your command. First, list the available volumes as follows.

Copy

Once you’ve identified which volume to remove, run the command below.

Copy

It’s worth noting that deleting a container doesn’t delete the previously linked volume. Instead, the volume is left in a dangling state. You can check the dangling docker volumes as follows.

Copy

Next, prune them to get rid of them.

Copy

Sometimes, you may wish to remove a container and its associated volume. For that, add the -v option to run the command below.

Copy

3) How to Remove Docker Images

You can list all the docker images in your workspace with the command below.

Copy

You will get an output showing the name of the docker images, their tags, and the image ID. Moreover, you will see the sizes for the images and the timestamps for when they were created.

To remove a docker image quickly, specify its name as in the example below.

Copy

If you only want to remove the dangling images, start by listing them.

Copy

For this example, we have no dangling docker images.

Next, prune them to get rid of all the dangling images

Copy

If you want to eliminate all the images, run the command below.

Copy

Conclusion

Docker is one containerization tool you can use with your application. With docker, you can create images, containers, and volumes. When you no longer need the created images, containers, and volumes, there is a quick and efficient way of removing them. This post has shared the commands to run based on your situation. That’s it!

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price