Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop docker containers with name matching a pattern

Tags:

docker

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)?

like image 976
y2k-shubham Avatar asked Jul 11 '19 23:07

y2k-shubham


People also ask

How do you stop a container with name?

We can feed the output of the docker container ls command into docker container stop by running docker container stop $(docker container ls -q --filter name=myapp*) .

How do you gracefully stop a docker container?

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.

How do you stop a container using container ID?

Stopping the Docker container is different from stopping any processes. To do so, you will need to use the “docker stop” command and specify the name or ID of the container that you want to stop.

How do you prune docker?

The docker system prune command is a shortcut that prunes images, containers, and networks. Volumes are not pruned by default, and you must specify the --volumes flag for docker system prune to prune volumes. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.


3 Answers

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)
like image 137
y2k-shubham Avatar answered Oct 19 '22 11:10

y2k-shubham


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.

like image 36
ryuzakyl Avatar answered Oct 19 '22 10:10

ryuzakyl


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

like image 29
ShivaGaire Avatar answered Oct 19 '22 11:10

ShivaGaire