You can use the Docker container rm or Docker rm command to remove or delete Docker containers. However, before you remove a container, you need to make sure that the container is not actively running. You can stop the containers using the Docker stop command before removing the containers. Another workaround is that you can use the –force option to forcefully remove containers. If you want to delete or remove all containers together, you can use a sub-command to list all container IDs along with the Docker rm command.
In this article, we will discuss all the methods to remove containers from the machine. Before we move ahead, you can check out our complete and free Docker Tutorial.
Docker Container Remove Command
You can use the below command if you want to delete or remove one or more containers.
$ docker container rm [OPTIONS] CONTAINER [CONTAINER...]
You can also omit the container keyword and still get the same output.
$ docker rm [OPTIONS] CONTAINER [CONTAINER...]
Before you can remove the containers, you need to make sure that the containers are not actively running. You can either stop all the containers and then execute the Docker rm command or simply use the –force option to forcefully remove the containers. There are several options that you can use along with this command. They are –
|-f||It uses the SIGKILL signal to forcefully remove a running container.|
|-l||You can also remove the links from the container.|
|-v||This option can be used to remove the volumes associated with the specified container.|
How to remove Docker containers?
Let’s first list all the active containers. You can use the following command.
$ docker ps
Now, let’s stop this container first using the Docker stop command.
$ docker stop myubuntucopy
You can see that the myubuntucopy container is not actively running. We can now easily remove this container using the following command.
$ docker rm myubuntucopy
You can see that the container has been removed successfully. You can also mention more than one container names or IDs separated by spaces to delete one or more container together.
Remove Containers Forcefully
Instead of stopping the containers first, you can directly use the –force option to forcefully remove the containers.
$ docker rm -f myubuntu
You can see that we have forcefully removed the containers.
Remove all Docker Containers
You can also remove all the Docker containers together at once. To do so, you can use the following set of commands. To stop all containers together, you can use –
$ docker stop $(docker ps -q)
You can see that all the active containers have been stopped. You can now use the following command to remove all Docker containers.
$ docker rm $(docker ps -aq)
You can see that all the containers have been stopped and removed successfully.
If you want to directly remove all containers without stopping them, you can use the –force option.
$ docker rm -f $(docker ps -aq)
All the containers have now been removed forcefully.
Remove all stopped containers
If you want to remove all the stopped containers, you can use the following command.
$ docker rm $(docker ps --filter status=exited -q)
Here, we have used a sub-command to list all container IDs of containers that are in exited state.
You can see that all the stopped containers have been removed successfully. Apart from this, you can also use the Docker Container prune command to remove all stopped containers.
$ docker container prune
We can use the Docker container rm or Docker rm command to remove Docker containers. To remove all the containers together or remove only stopped containers, we can provide suitable sub-commands in place of container ID in the parent Docker rm command. Also, if you don’t want to stop containers before removing them, you can remove the containers forcefully using the –force option.
If you have any queries or suggestions, please mention them in the comment box and we will have our experts get back to you as soon as possible. Also, check out our complete and free Docker Tutorial.