Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop and remove all docker containers

Tags:

docker

How can I stop and remove all docker containers to create a clean slate with my Docker containers? Lots of times I feel it is easier to start from scratch, but I have a bunch of containers that I am not sure what their states are, then when I run docker rm it won't let me because the docker container could still be in use.

like image 581
jjbskir Avatar asked Jul 27 '17 17:07

jjbskir


People also ask

How do you remove or stop all containers?

Use the docker container prune command to remove all stopped containers, or refer to the docker system prune command to remove unused containers in addition to other Docker resources, such as (unused) images and networks.

How do I stop all docker containers?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.


2 Answers

Stop all the containers

docker stop $(docker ps -a -q) 

Remove all the containers

docker rm $(docker ps -a -q) 

Find more command here

like image 152
John Kuriakose Avatar answered Oct 16 '22 02:10

John Kuriakose


Docker introduced new namespaces and commands which everyone should finally learn and not stick to the old habits. Here is the documentation, and here are some examples:

Deleting no longer needed containers (stopped)

docker container prune 

Deleting no longer needed images

which means, that it only deletes images, which are not tagged and are not pointed on by "latest" - so no real images you can regularly use are deleted

docker image prune 

Delete all volumes, which are not used by any existing container

( even stopped containers do claim volumes ). This usually cleans up dangling anon-volumes of containers have been deleted long time ago. It should never delete named volumes since the containers of those should exists / be running. Be careful, ensure your stack at least is running before going with this one

docker volume prune 

Same for unused networks

docker network prune 

And finally, if you want to get rid if all the trash - to ensure nothing happens to your production, be sure all stacks are running and then run

docker system prune 
like image 39
Eugen Mayer Avatar answered Oct 16 '22 01:10

Eugen Mayer