Open In App

What Is Docker rm Command ?

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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>

docker rm

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

docker rm multi container

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.

Docker rm -f

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)
remove_all_stopped_container
  • 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)

docker rm all the contaienrs

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
dockerps-a-f

Remove Exited Containers

To remove exited docker containers we can use the following command:

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

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
List_Created_or_Exited_Containers

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_Created_or_Exited_Containers

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
List_Containers_by_Pattern

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_Containers_by_Pattern

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.

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.

Remove_container_using--link

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
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
remove_single_docker_image

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 
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_multiple_docker_images

Remove All Docker Images

To remove all images from your Docker system, you can use the following command:

docker rmi $(docker images -q)
remove_all_dockerimages

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'
List_dockerimages_based_on_pattern

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_docker_images_based_on_filtered_pattern

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
docker_pull_ubuntu

Create a Dockerfile

Create a Dockerfile that will use the base image. You can add some instructions to modify the image.

Dockerfile_ubuntu_1

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 .
dockerbuild-t-my-image

Modify the Image

Make changes to the image, such as adding files or running additional commands. This will create new layers.

Update the Dockerfile

Dockerfile2

Rebuild the image without changing the tag

sudo docker build -t my-image:latest .
docker_build2

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
sudo_docker_rmi_myimage_latest

List Dangling Images

To list dangling images use the following command:

sudo docker images -f "dangling=true"
docker_dangling
  • 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
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
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
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
sudo_docker_volume_ls--f_danglingtrue
  • 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
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
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


Next Article

Similar Reads