Anton Semenichenko
Anton Semenichenko

Reputation: 354

Docker attach to grep re-started container name with a bash script

I have the following situation:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                             PORTS                    NAMES
9baf3a63f628        xxx_dj            "./script/docker-dj-…"   2 days ago          Up 14 seconds                                               xxx_dj_1_571c2893ba97
920b2d6d5816        xxx_app           "./script/docker-app…"   2 days ago          Up 13 seconds                      0.0.0.0:3000->3000/tcp   xxx_app_1_99d6a7cbe417
370d9c2b81fa        xxx_db            "/entrypoint.sh mysq…"   2 days ago          Up 16 seconds (health: starting)   0.0.0.0:3306->3306/tcp   xxx_db_1_811d236b8e68
203d50854c13        redis             "docker-entrypoint.s…"   2 days ago          Up 15 seconds                      0.0.0.0:6379->6379/tcp   xxx_redis_1_abef6a603298

I want to create a shorthand alias which will grep xxx_app_1_99d6a7cbe417 and launch it as a single command docker attach xxx_app_1_99d6a7cbe417

Please help me with the best solution, would be very handy and help someone else.

Upvotes: 0

Views: 122

Answers (1)

Jakub Bujny
Jakub Bujny

Reputation: 4628

Here you go (I'm not sure if that's what you need)

docker attach $(docker ps | grep -o "xxx_app_1_[0-9A-Za-z]*")

so that find for you xxx_app_1_99d6a7cbe417 - I suppose that you have problem that id 99d6a7cbe417 is changing :)

Upvotes: 1

Related Questions