-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhichami
More file actions
executable file
·29 lines (22 loc) · 810 Bytes
/
whichami
File metadata and controls
executable file
·29 lines (22 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# Detect the scale index and total count of this container service.
# Thanks! https://stackoverflow.com/a/64799824
# Usage
# whichami return the index number of this container.
# whichami --count return the total number of containers.
# get the container IP
IP=`ifconfig eth0 | grep 'inet ' | awk '{print $2}'`
# get the service name you specified in the docker-compose.yml
# by a reverse DNS lookup on the IP
SERVICE=`dig -x $IP +short | cut -d'_' -f2`
# the number of replicas is equal to the A records
# associated with the service name
COUNT=`dig $SERVICE +short | wc -l`
# extract the replica number from the same PTR entry
INDEX=`dig -x $IP +short | sed 's/.*_\([0-9]*\)\..*/\1/'`
# Echo index or count.
if [[ "${*}" == "--count" ]]; then
echo $COUNT
else
echo $INDEX
fi