How to Stop All Docker Containers with Single Command

In a home lab environment, the server is bound to go through more changes than conservatively-run enterprise counterparts. It is, after all, part of the experience. And one core difference in approach between the two environments is downtime. In the corporate world, server downtime is lost revenue. In the home lab world, well-architected downtime is yet another small project finished.

While it is entirely possible to stop Docker containers individually, I found the process rather tedious, especially when the projects I had planned would require multiple reboots and several other unplanned reboots to troubleshoot. I wanted all the containers to stop gracefully at a single command.

In your ~/.bashrc or ~/.zshrc, enter the following alias at the bottom of the file:

alias dstopall='docker ps -q | xargs -r docker stop && docker ps'

Calling dstopall does three things: get all running container IDs, pass them to docker stop, and print any remaining running containers. If everything went smoothly, nothing prints.

A tangent, the alias is designed to ignore shutdown order, because all containers are meant to come down regardless of dependencies. This wouldn’t be a problem under normal circumstances since the docker stop signal is sent to all containers simultaneously.

Leave a comment

Comments will be automatically closed after 30 days.