How to stop all docker containers at once

September 29, 2022 No comments docker containers bash console

1. Introduction

In this short article, we will present a way to stop and remove all docker containers at once, additionally, we show the command to remove all docker images as well.

2. How to stop all docker containers at once

To stop docker containers use the following command:

docker kill $(docker ps -q)

Let's break this command down into individual elements:

  • docker ps - will list all docker containers that are currently running,
  • -q - with this flag we will get only container IDs,
  • docker kill - a command that stops containers as a parameter we pass all container IDs.

3. How to remove all docker containers at once

Sometimes there is a need not only to stop the docker container but also to remove it from docker.

To remove all docker running containers simply use the following command:

docker rm $(docker ps -q)

To remove all containers even those not currently running use:

docker rm $(docker ps -a -q)

4. How to remove all docker images

To remove all docker images use the following command:

docker rmi $(docker images -q)

How this works:

  • docker images -q - will list all docker image IDs,
  • docker rmi - is a command to remove an image, we pass a list of image IDs to that command.

There are certain things I find myself googling over and over again. How to stop all Docker containers is one of those things! Hopefully, this post makes it just a bit easier to find.

5. Conclusion

Everyone who works with docker is constantly searching for these commands on the Internet so I hope this article will save you some time.

{{ message }}

{{ 'Comments are closed.' | trans }}