Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
If you are looking for how to stop and remove all running docker containers, here are a few simple commands to help. These are valid for docker compose also.
Docker commands are not OS specific so these should run fine on Ubuntu, Windows, or Mac.
There are multiple ways to achieve it. This blog post will list down many of them. Feel free to use whichever you seem to like.
Since we cannot remove a running docker image, we ill first stop all of them with a command, then delete images.
docker stop $(docker ps -a -q)
Once all the dockers are stopped, remove all docker containers by running the following command :
docker rm $(docker ps -a -q)
docker rmi $(docker ps -a -q)
docker volume ls -qf dangling=true | xargs -r docker volume rm
Herein we will be using the example on how to remove all the Redis instances only.
docker rm (docker ps -a |grep redis |awk '{print $1}')
[su_label type=”success”]Suggested read : [/su_label] Forever free blog with Google App Engine WordPress in 10 min
docker rm -f $(docker ps -a -q)
docker rmi -f $(docker images -q)
docker system prune --all --force --volumes
docker ps --filter 'status=Exited' -a | xargs docker stop docker images --filter "dangling=true" -q | xargs docker rmi
dstop() { docker stop $(docker ps -a -q); } alias dstop=dstop
sudo docker ps
sudo docker stop 9ab3de2442e2
sudo docker ps -aq
sudo docker system prune
Here’s a one-liner script to do all of these tasks :
docker ps | awk {' print $1 '} | tail -n+2 > tmp.txt; for line in $(cat tmp.txt); do docker kill $line; done; rm tmp.txt
[su_label type=”success”]Suggested read : [/su_label] How to install docker on ubuntu
I found an awesome script that also does a similar work of cleaning and removing all docker containers but more cleanly :
Let us know if you faced any issue or have better commands to share to stop or docker remove all containers, in the comments below.