How To Remove Docker Images, Containers & Volumes

Remove Docker images, containers and volumes

Docker is an open-source tool that helps you to build, test and deploy applications in containerized environments. However, there are lots of unused containers, images, volumes, and networks that may reside in your system. They consume a significant amount of disk space of the host operating system. Docker does not remove those objects without cleaning up it manually.

It is a good habit to clean up these unused disk spaces regularly and keep your system organized. Docker has several commands to clean up those unused objects.

In this tutorial, we will show you how to remove Docker images, containers, and volumes.

If you want to host Docker on Cloud and don’t know how to choose the best Cloud hosting provider. You can read my guide on The 8 Best Docker Hosting Platforms in 2022.

Prerequisites

  1. A Linux system with Docker installed and running.
  2. A root user or user account with sudo privileges.

How to Remove Docker Containers

A container is a running instance of an image. During the development process, there are a lot of containers are created and tested and exited. So it is necessary to find the unused containers and delete them immediately.

The basic syntax to remove Docker containers is shown below:

1
docker container rm container-name

Or

1
docker container rm container-id

First, list all running and stopped Docker containers using the following command.

1
docker container ls -a

You should see a list of all running and stopped containers in the following output:

show running and stopped containers

Remove a Stopped Docker Container

To remove the stopped container from the above list, run the following command:

1
dokcer container rm 908b35741c19

Where:

908b35741c19 is the id of the stopped container.

You can also remove the stopped container using their name.

To remove all stopped containers, run the following command:

1
docker container prune -f

You should get the following output:

remove all containers

Remove a Running Docker Container

To remove the running container, you’ll need to stop the container before removing it.

To stop the running container, run the following command:

1
docker container stop nginx-container

Once the container is stopped, remove it with the following command:

1
docker container rm nginx-container

Remove All Docker Container

If you want to remove all running containers, you will need to stop all containers first.

To stop all running containers, run the following command:

1
docker container stop $(docker container ls -aq)

Once all containers are stopped, you can remove them with the following command:

1
docker container rm $(docker container ls -aq)

How to Remove Docker Images

In this section, we will show you how to remove Docker images from the host system.

The basic syntax to remove the Docker images as shown below:

1
docker rmi image-id

Or

1
docker rmi [Repsoitory]:[Tag]

Remove Unused Docker Images

First, you will need to list all Docker images available in your system.

To list all Docker images, run the following command:

1
docker images

You should see all the Docker images in the following output:

list docker images

Next, locate the ID of the Docker image from the above output that you want to remove.

Then, run the following command to remove the unused Docker image using their ID:

1
docker rmi 3dd970e6b110

You should get the following output:

remove docker images

To remove the Docker images using their name, run the following command:

1
docker rmi httpd:latest

Where:

httpd is the Repository name and latest is the Tag name. You can find them using the “docker images” command.

Remove Used Docker Images

If you want to remove the Docker image that is used by any container. Then, you will need to stop and remove the container before removing the image.

First, let’s try to remove the Docker image that is used by any container:

1
docker rmi f35646e83998

You should get the following error:

1
Error response from daemon: conflict: unable to delete f35646e83998 (must be forced) - image is being used by stopped container 0b28392248ef

The above error indicates that any existing container uses the specified image.

In this case, you will need to stop and remove the container that uses that image:

1
2
docker container stop 0b28392248ef
docker container rm 0b28392248ef

Next, remove that image by running the following command:

1
docker rmi f35646e83998

Remove Dangling Docker Images

A dangling image means that you’ve created a new build of the image but haven’t given it a new name. You can remove the dangling image using the docker image prune command:

First, list all dangling images using the following command:

1
docker images -f dangling=true

You should get all dangling images in the following output:

1
2
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
                            9f94903c0336        17 seconds ago      72.9MB

To remove all dangling images, run the following command:

1
docker image prune

You should see the following output:

remove dangling images

Remove All Docker Images

You can remove all unused Docker images by just running the following command:

1
docker rmi $(docker images -a -q)

You should see the following output:

remove all docker images

How to Remove Docker Volumes

When you remove any container, a volume is not automatically removed at the same time. In that case, you will need to remove the volumes manually.

The basic syntax to remove Docker volumes as shown below:

1
docker volume rm volume-id

Or

1
docker volume rm volume1 volume2

First, list all volumes available in the host system with the following command:

1
docker volume ls

You should see a list of all volumes in the following output:

list docker volumes

To remove one or more unused volumes, run the following command:

1
docker volume rm volume1 volume1

To remove all unused volumes, run the following command:

1
docker volume prune -f

You should see the following output:

1
2
3
4
5
Deleted Volumes:
data1
data2
data3
data4

Recommended Reading

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva