Stop / remove all Docker containers
One liner to stop / remove all of Docker containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Written by Fabio Rehm
Related protips
32 Responses
My hard drive was full, total lockup, this was a saviour for me! thanks =D
that's great. I needed to stop and rm all containers before removing an docker image.
great command!
Having bookmarked this immensely useful tip a while ago, the two-liner above :) can be now done in one line:
docker rm -f $(docker ps -a -q)
And similar for all images:
docker rmi $(docker images -q)
docker rm $(docker images -q) or docker rmi $(docker images -q)
works just fine, after you stopped them
docker rm $(docker images -q) or docker rmi $(docker images -q)
works just fine, after you stopped them
sudo docker rm $(sudo docker ps -a -q)
In case of error message Get http:///var/run/docker.sock/v1.14/containers/json?all=1: dial unix /var/run/docker.sock: permission denied
I would add the -v flag:
docker rm -v $(docker ps -a -q)
else volumes will remain on the hard drive eating up space. Of course, be sure you want to delete your volumes and don't accidently delete data containers.
thanks!
Thanks! Fabulous!
To only stop exited containers and delete only non-tagged images.:
docker ps --filter 'status=Exited' -a | xargs docker stop
docker images --filter "dangling=true" -q | xargs docker rmi
Thank you. Very helpful!
Remove all containers that aren't currently running:
docker rm $(docker ps -a -q -f "status=exited*")
For anyone having trouble make sure you sudo the 2nd docker command:
sudo docker rm -f $(sudo docker ps -a -q)
sudo docker rm -f -v $(sudo docker ps -a -q)
if not the volumes will not be delete !!! (like if you are using a mysql docker image)
and all the volumes will be orphans !
Just a reminder - this command cannot be used via ssh like this:
ssh server@127.0.0.1 "docker stop $(docker ps -a)"
Because $(docker ps -a)
will be executed on your local machine, not on server.
The below removes all the Containers.
for f in docker ps -a -q
; do docker rm $f; done
Helped me a lot, thanks =D
You can use this as a bash alias:
dstop() { docker stop $(docker ps -a -q); }
alias dstop=dstop
With this, I can also delete the volume what container used to be mapped directly :) *if you didn't configure your local directory
Thanks man!!! I really appreciate it!!
Great Thanks
short and easy, thanks!
//remove all containers with keyword cluster
docker rm $(docker ps -a |grep cluster|awk '{print $1;}')
//remove all images with keyword cluster
docker rmi $(docker images |grep cluster|awk '{print $3;}')
If you want to stop them faster:
docker ps -a -q | xargs -n 1 -P 8 -I {} docker stop {}
With the latest docker 1.13, you can manage the container deletion in more efficient way. Check out this thread for the latest docker features for cleaning up images and containers http://discuss.devopscube.com/t/how-to-delete-all-none-untagged-and-dangling-docker-containers-and-images/23
thanks! good work
In Powershell, it's docker stop @(docker ps -a -q)
.
Also seems like this is a useful command to do cleanup:
docker volume prune
Why don't they just implement something like docker rm '*'
?
Following worked for me on my mac. This is combination from 2 other responses.
alias stopall='docker ps -a -q | xargs -n 1 -P 8 -I {} docker stop {}'
alias removeall='docker ps -a -q | xargs -n 1 -P 8 -I {} docker rm {}'
search docker-clean github.com
working solution, easy and simple