Open In App

What Is Docker rm Command ?

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

“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.

docker rm [OPTIONS] CONTAINER [CONTAINER…]

  • OPTIONS: Various options can be used with the docker rm command to modify its behavior.
  • 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 <Contaier ID>

docker rm

Removing 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 unstop every container at once, use the following command.

docker rm $(docker ps -aq)
  • docker ps -aq: This command lists all Docker container IDs in a quiet mode, which means it only outputs the container IDs without any additional information. Here’s what each part does:
  • docker ps: Lists all Docker containers.
  • -a or –all: Shows all containers, including stopped ones.
  • -q or –quiet: Outputs 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

People Also Read

Docker

Docker Commands

Read

Dockerfile

Read

Docker Tutorials

Read

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.

Docker rm Command – FAQ’s

How do I delete a file in docker?

To delete a file within a Docker container, you typically use the docker exec command to execute a shell command inside the container.

docker exec CONTAINER rm /path/to/example.txt

What is the command for RM images docker?

The command for removing Docker images is docker rmi. Here’s the basic syntax:

docker rmi [OPTIONS] IMAGE [IMAGE...]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads