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

To stop a running container we need to pass the container id or name to stop command i.e.

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

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

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.

Output:

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.

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.

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

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,

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

Now let’s forcefully remove this container without first stopping it,

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.

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.

It will forcefully remove both the containers.

Dry run of above used commands:

Get list of all containers,

Now remove running container with ID 8b38ae16205a

Now remove running container with Name : agitated_chaplygin,

Check if both the above mentioned running containers are removed i.e.

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.

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 LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.