6

Question:

How can I stop containers that their names start with server-?

Containers

> sudo docker-compose ps
                        Name                                      Command               State                        Ports                     
-----------------------------------------------------------------------------------------------------------------------------------------------
server-myservername1_1                                          some commands           Up                                               
server-myservername2_1                                          some commands           Up                                                     
server-myservername3_1                                          some commands           Up                            
server-myservername4_1                                          some commands           Up                     
server-myservername5_1                                          some commands           Up                   
server-myservername6_1                                          some commands           Up                                  
console-myconsolename1_1                                        some commands           Up                             
console-myconsolename2_1                                        some commands           Up                                      
Phoenix
  • 3,996
  • 4
  • 29
  • 40
  • Using bash loop commands [example](https://stackoverflow.com/questions/15065010/how-to-perform-a-for-each-loop-over-all-the-files-under-a-specified-path). – John Travolte Jan 18 '20 at 06:35

2 Answers2

9

First check the output of below command if it's gives the names of only those containers that you have to stop

docker-compose ps | grep server | awk '{print $1}'

If the list is right, then run

docker stop $(docker-compose ps | grep server | awk '{print $1}')

P.S. I haven't tested the above command. Let me know if it doesn't

Nitish
  • 597
  • 1
  • 4
  • 9
  • Thanks a lot, I use this command ``sudo docker-compose stop $(docker-compose ps --services | grep server |awk '{print $1}') `` – Phoenix Jan 18 '20 at 07:19
3

You can simply use --filter option of ps command
Suppose you wanna look for 3 containers, which their names start with site

docker ps --filter name=site*

will show you stat of those three containers.
so use one of this to stop the containers

- docker ps --filter name=site* -aq | xargs docker stop
- docker stop $(docker ps --filter name=site* -aq)