Stop all docker containers at once on Windows

DockerCommand Line

Docker Problem Overview


How can I stop all docker containers running on Windows?

docker stop is for 1 container only.

Any command/script to make it stop all containers?

Docker Solutions


Solution 1 - Docker

You could create a batch-file (.bat or .cmd) with these commands in it:

@ECHO OFF
FOR /f "tokens=*" %%i IN ('docker ps -q') DO docker stop %%i

If you want to run this command directly in the console, replace %%i with %i, like:

FOR /f "tokens=*" %i IN ('docker ps -q') DO docker stop %i

In Git Bash or Bash for Windows you can use this Linux command:

docker stop $(docker ps -q)

Note: this will fail if there are no containers running

For PowerShell, the command is very similar to the Linux one:

docker ps -q | % { docker stop $_ }

Solution 2 - Docker

For those who are interested this can be accomplished in Powershell using

docker ps -q | % { docker stop $_ }

Solution 3 - Docker

In PowerShell, you could also use this syntax

docker container stop $(docker container list -q)

Solution 4 - Docker

If the motivation of the question is to recover the memory occupied by Docker (in my case, this was why I arrived at this page), I found that the only way was to stop Docker Desktop completely. You do that by right-clicking the whale icon in the notification area (bottom right) > Quit Docker Desktop. When you restart Docker Desktop, all the containers reappear, and Docker even sets them to up again automatically.

Solution 5 - Docker

My two cents.

If you want to stop them filtered by some criteria

docker ps -a -q --filter "name=container_name" --format="{{.ID}}" | ForEach-Object -Process {docker stop $_ } 

or if you want to stop and remove them all together

docker ps -a -q --filter "name=container_name" --format="{{.ID}}" | ForEach-Object -Process {docker rm $_ -f}

By using pipe and foreach I avoid the error returned when there are no containers of this kind on the specific machine because docker stop or docker rm require at least one argument.

This script is used with combination of

docker container run image_tag --name=container_name

in order to use the filter later on when you want to stop and remove the containers.

Solution 6 - Docker

docker stop $(docker ps -aq)

to stop all running containers.

docker rm $(docker ps -aq)

to delete all containers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIssa FramView Question on Stackoverflow
Solution 1 - DockerhuysentruitwView Answer on Stackoverflow
Solution 2 - DockergreensmithView Answer on Stackoverflow
Solution 3 - DockerLuk AronView Answer on Stackoverflow
Solution 4 - DockerFrancisView Answer on Stackoverflow
Solution 5 - DockerOgnyan DimitrovView Answer on Stackoverflow
Solution 6 - DockerAlexandre K.View Answer on Stackoverflow