Unused Docker images can pile up over time and occupy considerable disk space. Removing these images efficiently helps recover storage and ensures your system remains clutter-free.
This guide will walk you through the process of removing unnecessary Docker images from your system. But before diving in, let’s briefly touch upon Docker, its images, and where these images are typically stored.
What is Docker?
Docker has become a robust and trustworthy tool for creating, testing, and launching applications since it was introduced more than ten years ago. It is a platform that is open-source and employs OS-level virtualization to enable the packaging of applications and their environments within containers.
What are Docker Images?
Docker containers are created using immutable templates known as Docker images. A Docker image holds all the necessary components for an application to operate, such as its code, dependencies, and configuration.
Containers made from these images are independent units that are lightweight, self-contained, and isolated, running separately from the host operating system. Containers, in contrast to virtual machines, are easier to move, effective, and require fewer resources as they do not rely on a standalone operating system. Containers ensure that the application performs consistently in various environments, regardless of deployment location.
Nevertheless, Docker containers and images have the potential to amass rapidly on your computer, taking up storage space. Fortunately, Docker provides several command-line tools to help manage and remove unused images and containers.
Where are Docker Images stored?
Docker images are often stored in remote repositories or registries, such as Docker Hub, the default registry, or third-party platforms like GitLab. Images can also be stored locally on your system's disk or in cloud-based repositories.
Prerequisites
For this tutorial, you will need a Linux environment with Docker installed and running. We’ll use Ubuntu 20.04 for our examples. If Docker is not yet installed on your system, you may want to follow a guide on how to install Docker on Ubuntu 20.04 before proceeding.
What is docker rm command?
"docker rm" is used to remove one or more Docker containers that are running on the Docker host. A Docker container uses up system resources like CPU, memory, and disk space when it is running. To free up such resources, you may choose to uninstall a container after using it for a while.
sudo docker rm [OPTIONS] CONTAINER [CONTAINER...]
- Options: Various options can be used with the docker rm command to modify its behaviour.
- Container: The name or ID of the Docker container(s) to be removed.
Step-By-Step Process To Remove Docker Container
Step 1: Provide a list of every container on the Docker host.
docker ps -a
Step 2: Stop the docker container before deleting it.
docker stop <Container name> or <Contaier ID>
Step 3: Remove the Docker container when it has stopped; you cannot remove the Docker container unless it has stopped.
docker rm <Container name> or <Container ID>
.webp)
How to Remove Docker Containers?
In this section you will get an idea about multiple ways to remove docker containers
Remove Multiple Docker Containers
You may easily remove a multi-container in Docker by using the following command.
docker rm container1 container2 container3
- docker rm: This is the Docker command for removing containers.
- container1 container2 container3: These are the names or IDs of the Docker containers
.webp)
Force Remove Docker Container
Even when a container is in operation, it can be forcefully removed by using the -f or --force option with the docker rm command. If you wish to forcefully remove a running container, you must use the -f option since Docker does not enable this by default.
docker rm -f CONTAINER_ID_or_NAME
You can see that I attempted to remove the Docker container both with and without the -f option in the following image.
.webp)
Remove All Stopped And Running Containers At Once
Remove All Stopped Containers
To remove all stopped containers in once, use this command.
docker rm $(docker ps -aq)
- docker ps -aq: Lists all Docker container IDs in quiet mode; that is, it prints only the container IDs and nothing else. Each part explained:
- docker ps: List all Docker containers.
- -a or --all: Show all containers, including stopped ones.
- -q or --quiet: Output only container IDs.
- $(...):This is command substitution in Bash. The output of the command inside the parentheses is substituted and executed in the outer command.
- docker rm: This command removes Docker containers.
Remove All running Containers
Add the -f argument to the command above.
docker rm -f $(docker ps -aq)
-(1).webp)
Remove All Exited Containers
To find and remove containers that have exited, first list the exited containers using a filter. After confirming which ones to delete, use the -q
flag to get only the container IDs, which can then be passed to the removal command.
List Exited Containers
To list exited docker containers we can use the following command:
sudo docker ps -a -f status=exited

Remove Exited Containers
To remove exited docker containers we can use the following command:
sudo docker rm $(docker ps -a -f status=exited -q)

Using Multiple Filters to Remove Containers
You can combine multiple filters to select containers that meet different conditions. For instance, if you want to delete containers that are either in a created state or have exited, use the following commands:
List Created or Exited Containers
To show list created or exited containers you can use the following command:
sudo docker ps -a -f status=exited -f status=created

Remove Created or Exited Containers
To remove created or exited containers we can use the following commands:
sudo docker rm $(docker ps -a -f status=exited -f status=created -q)

Remove Containers Based on Name Patterns
To delete containers that match a specific naming pattern, you can use grep
along with docker ps
. After confirming the list, use awk
and xargs
to provide the IDs for deletion. Note that grep
, awk
, and xargs
are not included with Docker and may need to be installed separately:
List Container by Pattern
To list containers that match a specific pattern, use the docker ps -a
command combined with grep
. This allows you to filter the list of all containers by a keyword or pattern.
sudo docker ps -a | grep "<pattern>"
Example
sudo docker ps -a | grep nginx:latest

Remove Containers by Pattern
To remove docker containers by matching pattern we can use the following command:
sudo docker ps -a | grep "nginx:latest" | awk '{print $1}' | xargs docker rm

Remove a Link Between Containers Using --link
Docker has the provision of connecting containers using the --link option, making it possible for direct communication between them. There might be certain situations, however, in which you will need to disconnect these two containers. For example, you want to make sure that the webapp-ubuntu container does not in any way talk to redis-ubuntu; in this case, you would unlink them.
You can do this by using the docker rm --link command. With this, you delete the previously set link, and in effect, kill the means of communication between these 2 containers. Something that comes in very handy when you have to work with managing relationships between containers on Docker’s default bridge network because then you get to have more control over how networks interact.
Unlink /webapp-ubuntu
from /redis-ubuntu
To remove the connection and prevent webapp-ubuntu
from communicating with redis-ubuntu
, use the following command:
sudo docker rm --link /webapp-ubuntu/redis-ubuntu
This command removes the link between the webapp-ubuntu
and redis-ubuntu
containers, cutting off the communication channel that was established using the --link
option.

How to Remove Docker Images?
To find the ID of Docker images on your system, you can use the following command, which lists all images along with their corresponding IDs:
sudo docker images
Remove Single Docker Image
Whenever you are trying to remove a Docker Image it's very easy but it is essential to make sure that you are removing the correct image.
Using the Docker RMI Command
If you want to remove a single Docker image, you can use docker rmi command, where "rmi" stands for "remove image".
To remove a specific image you can use that docker image ID. The below syntax will help you in understanding this command in a better way:
sudo docker rmi image_id
Example:
sudo docker rmi 11329d9fa885
Using the Docker Image RM Command
Both commands docker image rm and docker rmi commands works in a similar way & can be used in an interchangeable way to remove Docker images.
For example, to remove an image using this command, you would execute:
sudo docker image rm
Remove Multiple Docker Images
You can remove multiple images at once by providing multiple image IDs with the docker rmi
command:
For example, to delete images with the IDs a2a15febcdf3
and 4bb46517cac3
, you would run the following command:
sudo docker rmi d2c94e258dcb 35a88802559d
Remove All Docker Images
To remove all images from your Docker system, you can use the following command:
docker rmi $(docker images -q)
Remove Docker Images Based On a Pattern
You can filter and list images matching a specific pattern or tag using docker images
commands combined with grep command
.
Using Patterns to Identify Specific Images
To filter and list Docker images that matches a particular pattern, you can use the docker images
command combined with grep command
. For example, if you want to find all images associated with "my_nginx_image", you would run:
sudo docker images | grep 'my_nginx_image'
Remove Images Based on a Filtered Pattern
To remove a Docker image with "my_nginx_image" in its name, combine the commands as follows:
sudo docker rmi $(docker images | grep 'my_nginx_image' | awk '{print $3}')
Remove Dangling Images
To save disk space and use your Docker environment in better way, it is very important to clean unused dangling images or disk space. Before moving forward it is important to know about dangling images. Dangling images are Docker images that are not tagged or associated with an active or running container.Follow the steps below to create and remove dangling images:
Pull a Base Image
Pull a base image from a Docker registry. This image will be used to create another image that may become a dangling image.
sudo docker pull ubuntu
Create a Dockerfile
Create a Dockerfile that will use the base image. You can add some instructions to modify the image.
Build the Image
Build the image using the Dockerfile. Tag this image to ensure it's not dangling at this point.
sudo docker build -t my-image:latest .
Modify the Image
Make changes to the image, such as adding files or running additional commands. This will create new layers.
Update the Dockerfile
Rebuild the image without changing the tag
sudo docker build -t my-image:latest .
Create a New Tag for the Image
Create a new tag for the image, which will point to the same image layer.
sudo docker tag my-image:latest my-image:temporary
Remove the Tagged Image
Remove the tagged image, which will leave the layers as dangling images.
sudo docker rmi my-image:latest
List Dangling Images
To list dangling images use the following command:
sudo docker images -f "dangling=true"
- Dangling images are generally cleaned up automatically by Docker during a
docker system prune
operation, but you can also manually remove them if needed. - To remove dangling images, you can run:
sudo docker image prune
How to Remove Docker Volumes?
Deleting Specific Volumes
To delete one or more specific Docker volumes, you first need to identify them using the docker volume ls
command. Once you've located the target volume names, you can remove them with the docker volume rm
command:
List Volumes
To list docker volumes we can use the following command:
sudo docker volume ls
Remove Volumes
To remove multiple docker volumes we can use the following command:
sudo docker volume rm volume_name1 volume_name2
Example
sudo docker volume rm volume1 volume2
Remove Unused (Dangling) Volumes
Docker volumes are designed to persist beyond the lifecycle of the container they are attached to. When a container is deleted, the associated volumes are not automatically deleted. Volumes that are no longer attached to a container are called hanging volumes.
You can list these dangling volumes using a filter with docker volume ls. Once identified, you can remove them using the docker volume prune command.
List Dangling Volumes
To list dangling volumes we can the following command:
sudo docker volume ls -f dangling=true
docker volume ls
: This lists all the volumes on the Docker system.-f dangling=true
: This filter (-f
) ensures that only dangling (unused) volumes are displayed. "Dangling" refers to volumes that were once used by a container but are no longer in use, making them candidates for cleanup.
This command helps in identifying volumes that can be safely removed to free up disk space
Remove Dangling Volumes
To remove dangling volumes we can use the following command:
sudo docker volume prune
Remove a Container Along with its Volume
If you want to remove a container and its associated unnamed volume, you can use the -v
flag with the docker rm
command. This flag only works for unnamed volumes—when the container is deleted, the unnamed volume will also be removed automatically. Named volumes, however, will persist after container removal.
Remove Container and Unnamed Volume
sudo docker rm -v container_name
Example
sudo docker rm -v my_nginx_container
Conclusion
To sum up, the docker rm command is an effective tool for eliminating Docker containers from your computer. It enables you to remove containers that are no longer required, freeing up system resources. The name or ID of the container(s) you wish to delete should be specified in the command's basic format, docker rm [OPTIONS] CONTAINER [CONTAINER...]. The -f or --force option allows you to force the removal of running containers, however by default the operation simply removes halted containers. Furthermore, you can eliminate multiple containers, filter containers according to particular standards, or eliminate containers in bulk by combining different commands and parameters. Use caution at all times while executing the docker rm command, especially when the -f option is present, to prevent unexpected.
People Also Read
Docker |
---|
Docker Commands | Read |
---|
Dockerfile | Read |
---|
Docker Tutorials | Read |
---|
Similar Reads
What is Docker PS Command ?
he "docker ps" command is used to list all the running containers in the docker host. With the help of some filters, you can get the output of all the containers in the docker with are running and that are stopped. it shows the list of active containers that includes details about each one, includin
5 min read
What is Docker Cloud?
Docker is a software platform that provides some special kind of facilities, like a service provider that allows you to build, test, and deploy your application in centralized processing and quickly. So, the Docker Cloud is basically working as a service provider by Docker in which we can perform su
10 min read
What Is Docker Compose Up?
Docker Compose is a powerful tool utilized for defining and running multi-container Docker applications. It improves on the most common way of managing complex applications composed of multiple interconnected services by allowing you to characterize their configuration in a single YAML file.With Doc
15 min read
What Is Docker Daemon ?
Docker is synonymous with containerization, yet it is just one of the many implementations of the Open Container Initiative (OCI). As the most widely embraced containerization platform, Docker has greatly streamlined the development and deployment of modern applications. At the core of Docker's oper
6 min read
What Is Docker Client ?
Docker is rapidly growing in popularity. It is an open platform based on OCI, Open Container Initiative. The containerization helps separate applications from the underlying infrastructure. Thus, enabling rapid software development and release. Docker Client is the frontend that enables users to int
10 min read
What Is Docker Convoy Plugin ?
As a plug-in within the Docker ecosystem, the Docker Convoy, which is a swarm plugin, brings the advanced orbit of storage solutions to containerized apps. In the field of distributed systems, where a high level of resilience and accuracy of data is crucial, Convoy is evidenced as a trustworthy part
5 min read
What is Docker?
Have you ever wondered about the reason for creating Docker Containers in the market? Before Docker, there was a big issue faced by most developers whenever they created any code that code was working on that developer computer, but when they try to run that particular code on the server, that code
12 min read
Docker Prune Command
In the evolution of containerization, management of container environments is essential. In this docker prune a command provides a simplified approach of cleaning up unused resources from your containers. The importance of docker prune, and knowing how to improve resource usage by cleaning up the un
12 min read
What is Docker Build ?
Docker Build is one of the key features of Docker Engine, specifically designed to create container images. It plays an important role in the software development lifecycle by enabling developers to package applications and deploy them consistently across multiple environments. Docker Build isn't ju
12 min read
What is Docker Compose Override ?
Docker is a widely used platform designed to help developers build, share, and run container applications. We handle the tedious setup so that you can focus on the code service that offers free and premium tiers. The software that hosts containers is known as Docker Engine. The docker-compose.overri
5 min read