简体   繁体   中英

How to get Docker Compose/Docker Swarm/Kubernetes Container Number

All container orchestrators like Docker Compose/Docker Swarm/Kubernetes provide a way to set the number of containers to run. While running inside one of these containers, is it possible to determine the number of the container eg 5 of 10 where the number I'm looking for is 5. Is this exposed as an environment variable or is there another way to get this number?

In kubernetes you can get container names using jsonpath

containernames=$(kubectl  -n kube-system get pod <replacepodname> -o jsonpath={.spec.containers[*].name})

Then you can read the names one by one

echo $containernames | awk '{print $1 }' echo $containernames | awk '{print $2 }'

or

set -- $containernames echo $1 echo $2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM