Stop all docker containers at once on Windows - docker

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?

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 $_ }

For those who are interested this can be accomplished in Powershell using
docker ps -q | % { docker stop $_ }

In PowerShell, you could also use this syntax
docker container stop $(docker container list -q)

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.

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.

docker stop $(docker ps -aq)
to stop all running containers.
docker rm $(docker ps -aq)
to delete all containers.

Related

Docker stop all containers

I've seen many times the follwoing command to stop all docker containers:
docker stop $(docker ps -a -q)
There are two things that are not clear to me:
docker ps -a prints all containers, not only running ones, so what is the point to stop containers that are already stopped?
If docker ps returns/prints nothing (there are no running images) then docker stop blaims that it's not enough arguments.
What do I miss here? What is the best approach to cleanup an environment after docker?
use this will not run if the docker ps is empty:
docker ps -q | xargs --no-run-if-empty docker stop
normally you use rm and system prune if you really want to cleanup
You can also use this oneliner:
docker stop $(docker ps -aq | tr '\n' ' ')

How to kill multiple Docker containers of an image

Once in a while I bash into my containers to manually run some scripts on my servers and sometimes my session times out and those containers stay up.
I'm trying to run a command to kill all previous running containers of that image. Let's say my image is called "WEB" and this is what docker ps is showing:
ID NAMES
1 project_web_1
2 project_web_2
3 project_web_3
I want to kill all of those with a single command. I have tried this
docker rm -f $(docker ps -aqf "name=web") but this only kills the first one. Is there a way to kill all of them?
It seems my command actually removes all of them
docker rm -f $(docker ps -aqf "name=web")
I just had a typo in my image name. I leave this question/answer in case someone in the future needs it.
docker container rm -f $(docker container ps | awk '/yourname/ {print $1}')
Replace yourname with the name or anything you want to match from docker ps.

Docker cannot remove all images

i'm using Docker Desktop for Windows. When I try to remove all of my images by this command:
docker rmi $(docker images -q)
I got this error message in command prompt:
unknown shorthand flag: 'q' in -q)
I'm running Docker on Window 10 Pro
Syntax is unix specific, so you change your command for Windows and you'll need to run the whole command in PowerShell or CMD.
remove containers:
powershell "docker ps -a -q | foreach-object { $i = $PSItem.ToString(); $cmd = 'docker'; & $cmd 'rm' '-f' $i }"
remove images:
powershell "docker images -a -q | foreach-object { $i = $PSItem.ToString(); $cmd = 'docker'; & $cmd 'rmi' '-f' $i }"
Old question, but if you are on Windows switch from the cmd to PowerShell. cmd is misinterpreting some commands and tries to attach the -q to the outter rmi command.
Use the docker system prune -a or docker image prune -a commands to delete any unused or dangling images.
Look at similar issue. The syntax you are using is unix specific docker rmi $(command). This is called command substitution and probably will not work in windows.
In the windows, you should windows command.
In powershell, it'll be something like:
docker images -a -q | foreach-object { $i = $PSItem.ToString(); $cmd = "docker"; & $cmd "rmi" $i }
Where
docker images -a -q
gets the IDs of all images (including intermediate), and
foreach-object { $i = $PSItem.ToString(); $cmd = "docker"; & $cmd "rmi" $i }
delete them one-by-one
That's a bash shell syntax which would work on Linux installs of docker. To get it to work on Windows, try installing the bash shell on Windows and running it from inside that shell. The Windows command prompt and powershell won't understand that syntax.
Windows 10 Powershell, it is easy.
Following command shows all images:
docker -a -q
outputs this:
PS C:\Users\UserName> docker images -a -q
86fa138dd93d
7c3a8b83a719
6636e7059d8d
then, for each image do
docker rmi ##########
Where ##### is the image id
Save this in docker-clear.bat and execute to clear containers, images, unused data and volumes
#echo off
REM Clear all the containers
FOR /f "tokens=*" %%i IN ('docker ps -aq') DO docker stop %%i && docker rm %%i
REM Clear all the images
FOR /f "tokens=*" %%i IN ('docker images --format "{{.ID}}"') DO docker rmi %%i -f
REM Clear unused data
docker system prune -f
REM Clear all volumes
docker volume prune -f
You forgot the -a. To delete all the images, use this:
docker image prune -a -f
And remember that you have to remove all the associated containers before removing all the images.
To delete all containers in use:
docker container prune -f

How do I delete all running Docker containers?

I remember using
docker rm -f `docker ps -aq`
to chain the commands without an issue a few months ago, but now this isn't working, and I'm getting the following output:
unknown shorthand flag: 'a' in -aq`
See 'docker rm --help'.
What changed? How can I delete all Docker running containers in one line? In case it helps, I'm using Docker for Windows (native with Hyper-V, not with VirtualBox) on Windows 10, and the command I used has worked fine with my previous Windows 8 Docker toolbox installation.
Till now (Docker version 1.12) we are using the following command to delete all the running containers (also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command),
Delete all Exited Containers
docker rm $(docker ps -q -f status=exited)
Delete all Stopped Containers
docker rm $(docker ps -a -q)
Delete All Running and Stopped Containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove all containers, without any criteria
docker container rm $(docker container ps -aq)
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command,
docker system prune
All unused containers, images, networks and volumes will get deleted. Also, individually i.e. separately, we can do that using the following commands, that clean up the components,
docker container prune
docker image prune
docker network prune
docker volume prune
I had this issue when running in cmd. Switched to PowerShell and it worked!
Use:
docker rm -f $(docker ps -aq)
If anybody needs the Windows Shell Command (stop and remove container), here it is:
for /F %c in ('docker ps -a -q') do (docker stop %c)
for /F %c in ('docker ps -a -q') do (docker rm %c)
If you put it in a batch file, just add % to %c:
for /F %%c in ('docker ps -a -q') do (docker stop %%c)
I've had the same problem: I was on a Windows machine and used Docker within VirtualBox and the command docker rm -f ${docker ps -aq} worked well. Then I switched to Docker for Windows and the command didn't work on the Windows command line.
But using Cygwin or Git Bash solved the problem for me.
Try using this command.
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
$ docker rm $(docker ps --filter status=created -q)
Tested on Docker version 19.03.5, build 633a0ea on Mac OS Mojave.
Run docker commands in Windows PowerShell will execute and run most of the commands
Hope you also remember to stop running containers first before running the delete command
docker stop $(docker ps -aq)
Use this:
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
If you want to include previously stopped containers:
docker rm -f $(docker ps -a | grep -v CONTAINER | awk '{print $1}')
we can delete all running containers in docker ENV by the following the command -
docker container rm -f $(docker container ls -aq)
It should to the magic
if we have run our docker container using docker-compose.yaml then
docker-compose -f /path/to/compose/file down
should work
Single command to delete all stop and running containers (first stop and then just prune/remove them. Works for me all the time.
docker stop $(docker ps -a -q) && docker container prune -a
If the container is running, you cannot delete the image. First stop all the containers using the following command.
docker stop $(docker ps -aq)
you are saying running stop against the output of docker ps -aq.
'a' - get me all the containers
'q' - return only the container id.
Then run the following command to remove all the containers.
docker rm $(docker ps -aq)

single command to stop and remove docker container

Is there any command which can combine the docker stop and docker rm command together ? Each time I want to delete a running container, I need to execute 2 commands sequentially, I wonder if there is a combined command can simplify this process.
docker stop CONTAINER_ID
docker rm CONTATINER_ID
You can use :
docker rm -f CONTAINER_ID
It will remove the container even if it is still running.
https://docs.docker.com/engine/reference/commandline/rm/
You can also run your containers with --rm option (e.g. docker run --rm -it alpine), it will be automatically removed when stopped.
https://docs.docker.com/engine/reference/run/#clean-up---rm
Edit: The rm -f might be dangerous for your data and is best suited for test or development containers. #Bernard's comment on this subject is worth reading.
docker stop CONTAINER_ID | xargs docker rm
You can stop and remove the container with a single command the $_ gives you the last echo
docker stop CONTAINER && docker rm $_
In my case, to remove the running containers I used
docker rm -f $(docker ps -a -q)
In case you also need to remove the images, then run
docker rmi $(docker images -q) afterwards.
Only run docker rmi $(docker images -q) if you want to remove the images.
https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/
You can use kill, and also by using rm and the force flag it will also use kill.
Remove all containers: docker ps -aq | xargs docker rm -f
This will stop and remove all images including running containers as we are using -f
docker rmi -f $(docker images -a -q)
Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove
docker ps -a
To remove: $ docker rm ID_or_Name ID_or_Name
Remove a container upon exit:
If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits.
Run and Remove : docker run --rm image_name
Remove all exited containers:
You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you've verified you want to remove those containers, using -q to pass the IDs to the docker rm command.
List:
docker ps -a -f status=exited
docker rm $(docker ps -a -f status=exited -q)
Remove containers using more than one filter:
Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited, you can use two filters:
docker ps -a -f status=exited -f status=created
Stop and Remove all the containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
For removing a single container
docker rm -f CONTAINER_ID
For removing all containers
docker rm -f `docker container ps -qa`
To remove all stopped containers docker system prune
To stop live container, docker stop CONTAINER_ID waits 10 sec and it calls docker kill CONTAINER_ID
Or with docker kill CONTAINER_ID, you can immediately stop the container
remove all container with xargs
docker ps -a -q | xargs docker rm
for stop all
sudo docker ps -a -q |sudo xargs docker stop
remove single container
docker rm -f <container_id>

Resources