DEV Community

Adam Mateusz Brożyński
Adam Mateusz Brożyński

Posted on Edited on

How to stop multiple docker containers with same name prefix

If containers have names with same prefix, for example: test-service-1, test-service-2, test-something-more, then it's possible to stop them all at once:

$ docker ps -q --filter name=CONTAINER_NAME_PREFIX* \
    xargs docker stop
Enter fullscreen mode Exit fullscreen mode

If you'll add -a to docker ps, you can also change xargs docker stop to xargs docker container rm to remove all containers at once.

You can also stop and remove containers in one command:

$ docker ps -q --filter name=CONTAINER_NAME_PREFIX* | \
    xargs docker stop | xargs docker container rm
Enter fullscreen mode Exit fullscreen mode

Top comments (0)