skip to Main Content

I want to stop all containers that have dev1102in 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


  1. docker stop $(docker ps | grep -o 'dev1102_[A-Za-z_]*')

    should do the job!

    Login or Signup to reply.
  2. Another way is to pass the the output of docker ps | grep -o 'dev1102_[A-Za-z_]*' to docker stop command with xargs 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.

    Login or Signup to reply.
  3. I suggest to use restructured, new syntax of Docker CLI commands introduced in Docker 1.13. E.g. use docker container ls with filter flag, it is clean and more readable:

    docker stop $(docker container ls --all --quiet --filter "name=^dev1102_[A-Za-z_]*")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search