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.
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.
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.
Stop all the containers
docker stop $(docker ps -a -q)
Remove all the containers
docker rm $(docker ps -a -q)
Find more command here
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With