20

I'm using puckel/docker-airflow with CeleryExecutor. It launches a total of 5 containers named like this

docker-airflow_flower_1_de2035f778e6
docker-airflow_redis_1_49d2e710e82b
..

While development, I often have to stop all above containers. However, I can't do a docker stop $(docker ps -aq) since I have other containers running on my machine too.

Is there a way to stop all containers who's names match a given pattern (for instance all containers who's names start with docker-airflow in above)?

y2k-shubham
  • 10,183
  • 11
  • 55
  • 131
  • 1
    **See Also**: [Stopping Docker containers by image name](https://stackoverflow.com/q/32073971/1366033) – KyleMit Nov 18 '20 at 14:07

3 Answers3

29

From this article by @james-coyle, following command works for me

docker ps --filter name=docker-airflow* --filter status=running -aq | xargs docker stop

I believe docker CLI natively does not provide such a functionality, so we have to rely on filtering and good-old bash PIPE and xargs


UPDATE-1

Note that depending on your environment, you might have to do these

  • run docker commands with sudo (just prefix both docker .. commands above with sudo)
  • enclose name pattern in double-quotes --filter name="docker-airflow*" (particularly on zsh)
y2k-shubham
  • 10,183
  • 11
  • 55
  • 131
13

Better late than never ;). From this article. The following works for me:

Stop containers with names matching a given pattern:

$ docker container stop $(docker container ls -q --filter name=<pattern>)

On the other hand, if we want to start containers with names matching a given pattern:

$ docker container start $(docker container ls --all -q --filter name=<pattern>)

NOTE: For different environments related tips, @y2k-shubham's update is a good starting point.

ryuzakyl
  • 511
  • 8
  • 14
1

Another approach using grep and docker ps:

To stop docker container matching the given pattern/list of pattern":

docker ps | grep  -E "name_1|name_2|name_3"  |  awk '{print $1}' | xargs docker stop

To stop docker container excluding the given pattern/list of pattern:

docker ps | grep  -Ev "name_1|name_2|name_3" |  awk '{print $1}' | xargs docker stop

Reference: Grep

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31