forked from aanand/docker-wait
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait
executable file
·53 lines (43 loc) · 1.1 KB
/
wait
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/sh
set -e
DEFAULT_TIMEOUT=30
ports=$PORTS
# set the per host:port connection timeout
if [ -n "$TIMEOUT" ]; then
timeout=$TIMEOUT
else
timeout=$DEFAULT_TIMEOUT
fi
# empty ports variable will default to checking for exactly one port
if [ -z "$ports" ]; then
ports=$(env | grep _TCP_PORT | cut -d = -f 2)
if [ $(echo $ports | wc -w) -gt 1 ]; then
echo 'The linked container exports more than one port. Exiting'
exit 1
fi
fi
# find the host(s) associated with each of the ports
# and wait until that port responds
if [ -n "$ports" ]; then
echo "waiting for TCP connections"
for port in $(echo $ports | sed -e 's/,/ /g' -e 's/\s+/\n/g' | uniq)
do
for host in $(env | grep PORT_${port}_TCP_ADDR | cut -d = -f 2 | uniq)
do
seconds=0
while [ "$seconds" -lt "$timeout" ] && ! nc -z -w1 $host $port
do
echo -n .
seconds=$((seconds+1))
sleep 1
done
if [ "$seconds" -lt "$timeout" ]; then
echo -n "$host:$port up!"
else
echo -n "$host:$port WARN: unable to connect"
fi
echo -n '\n'
done
done
fi
exit