How to Stop/Remove multiple docker containers at once

When you are running a couple of containers, it’s okay to stop or remove containers one by one, but it’s not feasible when you are running multiple microservice containers.

In my instance, I have a bunch of microservices that get deployed and stacked together. In total, I have more than 25+ service containers running and now I want to stop them. What command should I run to stop or remove all Docker containers at once? 

It’s not a big deal; you can remove or stop containers at once by running a single command, which is the confluence of multiple commands.

Stop containers in Docker

I believe many new users may not be sure how to stop/remove single containers from Docker. Therefore, I’ll share with you the basic command to stop a single container from Docker, and after that, you will see how to stop multiple containers from Docker at once.

Stop Single containers in Docker

When you want to stop a Docker container, it’s necessary to know the container ID, but the question arises of how to get container information. It’s very simple. Just run the following command and copy the container ID:

docker ps 

For precise output, you can run the below command, which will omit other information except for the container ID:

docker ps -q
d9c75e035827
bef968c8cc70
cd9e7dc6ad3d
252b61c8195d
a3bd7afd45c6
********* SNIPPED OUT *********
7617dc0638b5
939d11a1b334
8326c8d47bb8

And once you know the container ID, run the below code to stop the respective container and make sure to replace it with an actual container ID:

docker stop [CONTAINER-ID]
            OR
docker kill [CONTAINER-ID]

It’s up to you whether you want to use stop or kill to halt your running containers. If you are using stop, then it will send a SIGTERM signal to stop the running container.

Therefore, it may take a couple of seconds to stop the containers. If you use kill, then it will send a SIGKILL signal to kill running containers, which is a brutal way to stop running containers. However, it all depends upon your circumstances.

Interrupt or Suspend a Command Execution in Linux

Stop Multiple containers in Docker

After following the above guide, you are completely aware of how to stop single containers and the process behind them.

Now let’s remove the multiple containers from Docker at once with a simple command. As you can see, the below command is self-explanatory, so I don’t think you need an explanation for this particular command.

docker stop  $(docker ps -aq)
            OR
docker kill $(docker ps -aq)

If you have not added Docker to the sudo group, then you need to run the below command. Alternately, you can add Docker to the sudo group by following the guide on creating a SUDO user in Linux and then you can run the above command:

sudo docker stop  $(sudo docker ps -aq)
            OR
sudo docker kill $(sudo docker ps -aq)

In response to the above command, the terminal will print the container IDs one by one after a successful exit of the running containers.

To verify, you can type the below command:

docker ps 

Remove containers from Docker

You may want to remove containers from Docker once they are no longer used. Similar to the above method, you can remove single or multiple containers from Docker at once by passing a single command.

Remove single container from Docker

Before removing containers, you should stop the container and then remove the containers. While removing a container from Docker, you will require the container ID, which you can get from the below command:

docker ps -a
            OR
docker ps -aq

Output:

CONTAINER ID   IMAGE                      COMMAND                  CREATED       STATUS                        PORTS     NAMES
bef968c8cc70   traefik:2.5                "/entrypoint.sh --pr…"   11 days ago   Exited (0) 59 minutes ago               appwrite-traefik
cd9e7dc6ad3d   appwrite/appwrite:0.13.3   "executor"               11 days ago   Exited (137) 25 minutes ago             appwrite-executor
********* SNIPPED OUT *********
06370db2c0d3   appwrite/influxdb:1.0.0    "/entrypoint.sh infl…"   11 days ago   Exited (0) 26 minutes ago               appwrite-influxdb

After getting the container ID, you can run the subsequent command, and don’t forget to update the container ID with the correct one.

docker rm [CONTAINER-ID]

Remove multiple containers from Docker

The above method is not reliable. What should you do if you want to remove all containers from your Docker machine? Don’t dwell much. You can use the below command to remove all the containers from your Docker system.

To execute the removal process, you can run the below command:

docker rm  $(docker ps -aq)
            OR
sudo docker rm  $(sudo docker ps -aq)

One by one, the container IDs will be printed on your screen, which can be interpreted as an indication that the execution has been completed successfully without any interruption.

To re-verify, you can run the below command, and you will not find a single container on your Docker system.

docker ps -a

Prune command to stop and remove all containers from Docker

The above method is a convenient way to stop and remove all containers from the system. However, there is another command that is more convenient than the traditional method.

A prune command helps you to remove all unnecessary items like images, containers, networks, volumes, etcetera that can be removed in one go without touching other running containers.

The syntax for removing all stop containers at once is very simple and interactive:

docker container prune
            OR
sudo  docker container prune

Interactive Output

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Deleted Containers:
b9fa43d897b9e25ebdc35d517b74d30b37e9961501ec78c3777fd96700868ade
debb43cb77f8f89279431d9bc2db3c3111c02f94c343c77279c2cd4b6ce1344e
57036f0011e5a2341647d9cf4e471e21153926e7fe2aac40d6983bc99ce00c30
b574f0cc20eafe55f031b828bca592e651ae420901edece4b905fdbe343bea03
bee407c6564b818c8baa316710f211579154f559c79649d8f702a979af7b7137
6d69524e099deeab3f6be51248ae971a327a7360c0e4e303dbd3914298f6ef28

Total reclaimed space: 0B

Wrap up

That’s all it takes to stop and remove multiple Docker containers, and you now know how to stop and remove single and multiple containers simultaneously with a special prune command.

Enjoy it!

Leave a Reply