I want to stop all containers that have dev1102
in their name. There are several containers but I never know how many. So I run docker ps |grep -o 'dev1102_[A-Za-z_]*'
which lists me all containers I need. Output looks like this
dev1102_abc_de
dev1102_admin
dev1102_app_qqq
How can I pipe this output into a docker stop command, so all the listed containers from the grep get stopped?
I tried docker stop < docker ps |grep -o 'dev1102_[A-Za-z_]*'
but that results in a bash error:
-bash: docker: No such file or directory
3
Answers
docker stop $(docker ps | grep -o 'dev1102_[A-Za-z_]*')
should do the job!
Another way is to pass the the output of
docker ps | grep -o 'dev1102_[A-Za-z_]*'
todocker stop
command withxargs
like this:docker ps | grep -o 'dev1102_[A-Za-z_]*' | xargs -I {} docker stop {}
Here
xargs -I {} docker stop {}
placeholder{}
is replaced by each container name.I suggest to use restructured, new syntax of Docker CLI commands introduced in Docker 1.13. E.g. use
docker container ls
withfilter
flag, it is clean and more readable: