In this TechRepublic How to Make Tech Work video, Jack Wallen shows how to stop and remove all Docker containers at once with just two simple commands.
I cannot tell you how many times Iāve had way too many Docker containers running and wanted to just blow them all away and start over. Granted, I wouldnāt do this on a production machine; but if youāre working on a development environment where it doesnāt matter if you kill every running container, then using this nuclear option is a good way to go.
For instance, you might need to deploy a new testing container on a port that some other test container is using. Instead of tracking that container down ā and as long as you donāt need any other container to stay running ā thereās a much easier solution for this. What Iām about to show you does exactly what it sounds like.
The first command will stop all running containers, and the second command deletes them. It works every time. Hereās how you do it.
To stop all of your running Docker containers, issue the command docker stop $(docker ps -a -q). The next command removes all containers, which is docker remove $(docker ps -a -q).
As you can see, there are two commands: docker remove (or stop) and docker ps -a -q. The first command uses the output of the second command as a variable, so if you have multiple containers running, itāll either stop or remove them all at once. This command comes in very handy, though remember to use it wisely. If you have any containers running that must remain up, youāre better off stopping and removing the containers manually.
Subscribe to TechRepublicās How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.