firasKoubaa
firasKoubaa

Reputation: 6867

Shell : How to get a container's name containing some string

I have a list of containers where names are like following :

container 1: myApp_ihm.dfgdfgdfgdfvdfdfbvdfvdfv

container 2: myApp_back.uirthjhiliszfhjuioomlui ...

container 3: myApp_database.piyrjfhjyukyujfkgft

I have to execute some string on the container where the name contains ihm (the first one in my example)

In order to exec my commands , I'm used to do:

 docker exec -it ihm bash

so ihm should by replaced by some test to get the first one name :

myApp_ihm.dfgdfgdfgdfvdfdfbvdfvdfv

Suggestions?

Upvotes: 1

Views: 1935

Answers (3)

mayorsanmayor
mayorsanmayor

Reputation: 2988

This worked for me, added that to a bash script and saved myself 30-60 seconds of typing/copy-pasting every time I want to go into my container.

docker exec -it $(docker ps --format "{{.ID}} {{.Command}}" | grep /home/app/ | awk '{print $1}') /bin/bash

Upvotes: 0

firasKoubaa
firasKoubaa

Reputation: 6867

docker exec -it $(docker ps --format "{{.Names}}" | grep "ihm") bash

Upvotes: 2

Kilian
Kilian

Reputation: 1781

docker exec -it $(docker ps | grep myApp_ihm | awk '{print $1}') /bin/bash

Upvotes: 7

Related Questions