Docker: How to Stop and Remove All Containers at Once

It’s an exaggeration to say that Docker is a game-changer for systems engineers and developers. You can run almost any application with a single command and customize it to your environment via a static container-based interface. But as containers proliferate, controlling them also becomes more complex. Managing containers from the command line can be a pain, but setting up a synchronization tool like Kubernetes or Docker Swarm is overkill for smaller systems.

Stopping and removing a container from the command line requires two steps. Turn off and take out two containers four. And stopping and removing 10 containers – well, you have the idea. Let’s look at how to make the Docker command line easier to use. We will focus on stopping and removing containers. Next, we’ll take a look at Docker Compose, another tool that makes managing smaller batches of containers easier.

These examples would be for systems that use shell-based Docker tools, including macOS, Linux, and Windows with WSL. You will need to have Docker installed in addition to configuring the docker.

Stop and unpack all containers


Here is a command that will stop and remove all containers on your system, assuming the user is running it root or a member of docker Collection.

$ docker ps -aq | xargs docker stop | xargs docker rm

How does this work? Let’s take a closer look.

container list

The first part of the command lists all containers on the system. Here is a screenshot of a system with four containers:

the -q option says docker note to list all containers (-a(by container id)-Q). You can combine the two arguments after one dash ().

If you drop a file ayou see only three containers:

This is because one of them is currently not working. This is a long list:

This is the other half of the screen. You’ll want to come back to it later.

So, if you want to stop all running containers and remove everything, regardless of its previous state, provide –a to docker note.

What is Xargs?

The next two parts of the command to stop and remove all containers begin with xargs. Xargs is a Linux tool that accepts entries from its input and executes the command you specify for each entry.

Here’s an example that gives you an idea of ​​what happens when you pipe a pipe docker ps -aq to xargs.

Remember the output from docker ps -aq.

When you pass it to xargs Without any additional arguments, it defaults to / bin / echo to his command, and append his input to the end of the command you specify:

About this command output docker note to:

echo 344bf90e09e7
echo 8667dc69816a
echo 322f55c7b223
echo c5df9ef22d09

because you didn’t tell sound echo To add a carriage return, it printed all 4 identifiers on one line.

This is not a very useful example. So, let’s get fancy.

docker port Lists information about network ports in a container.

You need to pass the container id and port you are interested in. So, we need to tell us that xargs How to run this command.

Here’s how to check port 80 on all four containers.

First, this command line works docker ps -aq.

then pipe (|) output to xargs with -I ‘ID’ As the first two arguments. this says xargs that when he sees ‘Identification card’ In the next command, replace it with an input from the tube.

So, xargs converts the command Docker port ‘ID’ 80 In these four commands:

docker port 344bf90e09e7 80
docker port 8667dc69816a 80
docker port 322f55c7b223 80
docker port c5df9ef22d09 80

The output of these four commands shows us that Docker has mapped three of the containers to ports 8083, 8082, and 8081. The fourth container does not contain a port where it has never finished starting.
Error: No public port ‘80/tcp’ published for 322f55c7b223

Scroll to the beginning of this post to see the output docker note a To view appointments.

Stop and unpack all containers

Now we know how the rest of it works.

$ docker ps -aq | xargs docker stop | xargs docker rm

It will be expanded to:

docker stop 344bf90e09e7
docker stop 8667dc69816a
docker stop 322f55c7b223
docker stop c5df9ef22d09

docker rm 344bf90e09e7
docker rm 8667dc69816a
docker rm 322f55c7b223
docker rm c5df9ef22d09

Let’s try it on beta:

Success! rm . docker Repeat the container identifiers as you have deleted them. Since you cannot delete a running container, the stop commands should work.

This is a great way to stop and remove a batch of containers.

Removed with extreme bias

There is a shorter, more concise and less friendly way to stop and remove all containers on the system.

$ docker ps -aq | xargs docker rm -f

This works docker rm -f on each container. It’s an easier way to remove containers, but you probably don’t want to use them.

Here’s why: in order to stop the container, Docker has to stop the process it’s running inside. It does this by sending the application a Signal. A signal is a notification to the Linux process that something has happened. In this case, the signal means that it is time to close.

But not all signs are equal.

docker stop Sends will be fined. Apps have the option to ignore, block or deal will be fined. Well-behaved applications take advantage of this opportunity to shut down any threads or child processes and perform basic house cleaning tasks. For example, a server application can notify clients that it will disappear, or a database can clear any unsaved transactions.

docker rm -f Sends sekil. Processes cannot block or manipulate this signal. They are terminated immediately, with absolutely no chance of doing any housekeeping. This could lead to data loss.

docker stop will only use sekil If the application is not closed in a reasonable period of time.

Therefore, using to stop instead of rm -f Good idea, whether you’re parking a bunch of containers or just one at a time.

Docker Compose

If you don’t want to spend a lot of time on the command line managing containers, or if you have a bunch of containers that need to talk to each other, Docker Compose might be a better option than the command line.

Docker Compose uses a YAML configuration file to configure, start, and stop containers. Let’s look at a simple file that starts with three containers:

# docker-compose.yml
version: '3.7'
image: docker/getting-started
user: root
- 8081:80
container_name: one
image: docker/getting-started
user: root
- 8082:80
container_name: two
image: docker/getting-started
user: root
- 8083:80
    container_name: three

Each container is defined as a file Services. This file defines three containers, named OneAnd twoAnd And three. Like the example above, They run Docker start Picture and map port 80 to 8081, 8082, and 8083 respectively.

Create this file in an empty directory. then say docker compose to get started docker compose up d.

I’ve created the three containers, and you can see them working with them ps operator -a.

Stopping them is just as easy. Simply turn on docker compose In the same directory.

docker composeThe output is more user-friendly than docker‘s. Stop all containers included in the file. ps . docker Confirms they are gone.

This is only the tip of the iceberg when it comes to the capabilities of Docker Compose. You can use it to define containers and volumes for persistent data storage, control access, private network for containers to communicate with, and more.

Packaging: stop and unpack

In this post, you have seen how to stop and remove all containers on your system with one command. You have learned how to use xargs Tells Docker to run the same command on a group of containers. I also had a look at the cute and not so cute ways to close a container app.

Then we covered Docker Compose and how you can use it to manage a set of containers. You’ve seen how to replace the command line examples we covered with a YAML configuration file and a pair of simple commands.


Leave a Comment