2

I'm trying to stop all the containers starting with the name app_

I though this would work: docker stop $(docker ps -f name="app_*"), but it shows:

unknown shorthand flag: 'f' in -f
See 'docker stop --help'.

Is there a way to do this?

2 Answers 2

8

You must put the complete filter expression into parantheses:

docker ps -f "name=app_*"

The search is fuzzy by default, so e.g. name=app will also return my-app. You can use a regex to indicate that the match should be at the start:

docker ps -f "name=^app_"

You should further add the quiet flag q so that the command only returns ids to make it work with docker stop:

docker stop $(docker ps -qf "name=^app_")
2

Your command would work, if you had the docker ps show only the ID of containers.

I started some sleeping containers like this:

for i in $(seq 1 10); do 
   docker run --rm -d --name sleep_$i bash sleep 100000; 
done

And since they were all named sleep_* I could stop them like this:

docker ps -f Name=sleep_* --format '{{.ID}}' | xargs  docker stop

I usually use xargs rather than $() when possible for this kind of thing - for one reason, I can easily parallelize it with -n 3 -P 10 (10 concurrent executions of 3 each):

$ docker ps -f Name=sleep_* --format '{{.ID}}' | xargs -n 3 -P 10  docker stop
c58a1fc538cf
98a9358734e4
e34828d3a7d7
ffbd2ec18775
1ba1e8a304e7
5e78aecb4ed6
cf86c4ea46aa
624a7d0342c2
7cb17ece1f0a
01665a084d02

Quite a lot faster for many containers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.