Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

shell - How to get services' container ids in cluster using docker compose?

I've started multiple container services using docker-compose with following command -

docker-compose up -d --scale cluster-service=3

This starts three container services -

CONTAINER ID        IMAGE                                        COMMAND                  CREATED             STATUS              PORTS                                              NAMES
87b60bd2554c        xxx.xxx.xxx.xxx:5000/cluster-service:1.1.1   "java -cp cluster-se…"   7 minutes ago       Up 7 minutes        0.0.0.0:9077->47100/tcp, 0.0.0.0:9076->47500/tcp   testcluser_cluster-service_1
b631a656f8ae        xxx.xxx.xxx.xxx:5000/cluster-service:1.1.1   "java -cp cluster-se…"   7 minutes ago       Up 7 minutes        0.0.0.0:9075->47100/tcp, 0.0.0.0:9074->47500/tcp   testcluser_cluster-service_2
f3aeb9541fe9        xxx.xxx.xxx.xxx:5000/cluster-service:1.1.1   "java -cp cluster-se…"   7 minutes ago       Up 7 minutes 0.0.0.0:9073->47100/tcp, 0.0.0.0:9072->47500/tcp   testcluser_cluster-service_3

In Jenkins pipeline, using shell script, I want to get container ids in the cluster. How can I achieve this with shell script?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Utilise awk to filter on the name and so:

docker ps | awk '$NF~/cluster_service/ { print $1 }'

Check to see if the last space separated field ($NF) matches "cluster-service" If it does, print the container ID (field 1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...