In this article we will discuss how to stop and remove a running container by filtering on ID and Name.

To completely remove a running container, first we have to stop it and then remove it.

How to stop a running docker container ?

Suppose we have a running container whose Status is ‘UP’ i.e

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d857536373e3        centos              "/bin/bash"         3 minutes ago       Up 3 minutes                            musing_bose

To stop a running container we need to pass the container id or name to stop command i.e.
docker stop <container IDs | container names >

Now let’s stop the above mentioned running container by container ID i.e.
docker stop d857536373e3

Alternatively to stop the container we can pass the container name too i.e.
docker stop musing_bose

Both the above commands will stop the running container, but container is not removed from the system yet. It still exists in system and its status is ‘Exited‘. Let’s fetch the list of all containers to check the status of stopped container i.e.
docker ps -a

Output:
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
d857536373e3        centos              "/bin/bash"         34 minutes ago      Exited (137) 1 minutes ago                       musing_bose
4622f48ceaed        ubuntu              "/bin/bash"         34 minutes ago      Exited (0) 34 minutes ago                        hungry_hofstadter
ecc42527bba4        ubuntu              "/bin/bash"         35 minutes ago      Exited (0) 35 minutes ago                        sharp_kapitsa
6f5190548f7a        centos              "/bin/bash"         35 minutes ago      Exited (0) 35 minutes ago                        loving_cocks
6708084317a1        centos              "/bin/bash"         35 minutes ago      Exited (0) 35 minutes ago                        jovial_diffie
af70224e7cc5        centos              "/bin/bash"         35 minutes ago      Exited (0) 35 minutes ago                        objective_haibt
121ba587209b        centos              "/bin/bash"         35 minutes ago      Exited (0) 35 minutes ago                        flamboyant_nobel

In the first line we can see the status of container with ID d857536373e3 is ‘Exited‘.

How to removed the stopped / Exited docker container ?

Now to remove the container completely from the system we need to use docker rm command i.e.

docker rm <container IDs | container names>

It will remove the one or more stopped containers based on IDs or Names provided.

Let’s remove the recently stopped container by container ID i.e.

docker rm d857536373e3

Alternatively to remove the container we can pass the container name too i.e.
docker rm musing_bose

Both the above commands will remove the container with ID d857536373e3 from our docker engine. If we try to remove any running container (status – UP) using docker rm command, then it will through error like this,
Error response from daemon: You cannot remove a running container 1a600547f8f52a57d03f04667b9e8497bf5d2d37b85696b16918ca1187831e0e. 
Stop the container before attempting removal or force remove

So one way is to stop and then remove container. Also, as mentioned in above error string, there is an an another way to directly remove a running container.

Directly Stop & Remove a running container by force in single command

In docker rm command we can also provide –force or -f option to forcefully remove the containers (internally uses SIGKILL).

Suppose we have a running container i.e.

PS C:\Varun> docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1a600547f8f5        centos              "/bin/bash"         4 minutes ago       Up 4 minutes                            hungry_kapitsa

Now let’s forcefully remove this container without first stopping it,
docker rm -f 1a600547f8f5

It will directly stop & remove the container from docker engine. Now if we fetch the list of all containers in our system, then this container will not be there i.e.
PS C:\Varun> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

We can also remove multiple containers using docker rm command i.e.

Suppose we have two running containers with ID 1a600547f8f5 and 3ab005456123. Now to remove both the containers we can pass their ID to rm command i.e.

docker rm -f 1a600547f8f5 3ab005456123

It will forcefully remove both the containers.

Dry run of above used commands:

Get list of all containers,

PS C:\Windows\system32> docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0d68f20be559        ubuntu              "/bin/bash"         5 seconds ago       Exited (0) 4 seconds ago                        cranky_keller
494355aa24f6        ubuntu              "/bin/bash"         11 seconds ago      Exited (0) 9 seconds ago                        vibrant_tu
5d79bd7be240        centos              "/bin/bash"         16 seconds ago      Exited (0) 14 seconds ago                       hopeful_feynman
7e212f6b8780        centos              "/bin/bash"         18 seconds ago      Exited (0) 17 seconds ago                       gifted_chandrasekhar
338b3c461ae7        centos              "/bin/bash"         21 seconds ago      Exited (0) 20 seconds ago                       mystifying_williamson
4e88ee8e736e        centos              "/bin/bash"         About an hour ago   Up About an hour                                agitated_chaplygin
8b38ae16205a        ubuntu              "/bin/bash"         About an hour ago   Up About an hour                                silly_napier

Now remove running container with ID 8b38ae16205a
docker stop 8b38ae16205a
docker rm 8b38ae16205a

Now remove running container with Name : agitated_chaplygin,
docker stop agitated_chaplygin
docker rm agitated_chaplygin

Check if both the above mentioned running containers are removed i.e.
PS C:\Windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
0d68f20be559        ubuntu              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       cranky_keller
494355aa24f6        ubuntu              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       vibrant_tu
5d79bd7be240        centos              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       hopeful_feynman
7e212f6b8780        centos              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       gifted_chandrasekhar
338b3c461ae7        centos              "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       mystifying_williamson

Containers with ID 8b38ae16205a and Name agitated_chaplygin are not found now. So deleted completely.

Now to remove all the stooped containers we don’t need to pass -f option i.e.

docker rm 0d68f20be559 494355aa24f6 5d79bd7be240 7e212f6b8780 338b3c461ae7

It will delete 5 containers which were in exited state.

But if want to delete 100 containers or remove containers based on conditional filtering then it will be really difficult to pass their container ID one by one. So, in next article we will discuss how to stop & remove multiple containers in a single command.

Join a list of 2000+ Programmers for latest Tips & Tutorials