Browse Source

fix: use a docker container to check for open tcp port

This will allow to dockerize compose in the future and makes
it more portable. Indeed, the containers are not always reachable
from the host.
test
Valentin Lab 6 years ago
parent
commit
7a779015b0
  1. 21
      bin/compose

21
bin/compose

@ -695,19 +695,26 @@ wait_docker_ip() {
}
export -f wait_docker_ip
DEFAULT_BASH_IMAGE=${DEFAULT_BASH_IMAGE:-docker.0k.io/bash}
wait_for_tcp_port() {
local host_port=$1 timeout=30 start=$SECONDS
local network=$1 host_port=$2 timeout=20
verb "Trying to connect to $host_port"
docker run --rm -i --network "$network" "$DEFAULT_BASH_IMAGE" <<EOF
start=\$SECONDS
while true; do
timeout 1 bash -c "</dev/tcp/${host_port/://}" >/dev/null 2>&1 && break
sleep 0.2
if [ "$((SECONDS - start))" -gt "$timeout" ]; then
err "${RED}timeout error${NORMAL}(${timeout}s):"\
"Could not connect to $host_port."
return 1
if [ "\$((SECONDS - start))" -gt "$timeout" ]; then
exit 1
fi
done
exit 0
EOF
if [ "$?" == 1 ]; then
err "${RED}timeout error${NORMAL}(${timeout}s):"\
"Could not connect to $host_port."
return 1
fi
return 0
}
export -f wait_for_tcp_port
@ -726,7 +733,7 @@ wait_for_docker_ip() {
while read-0 port; do
IFS="/" read port type <<<"$port"
[ "$type" == "tcp" ] || continue
wait_for_tcp_port "$DOCKER_IP:${port}" || return 17
wait_for_tcp_port "$DOCKER_NETWORK" "$DOCKER_IP:${port}" || return 17
verb "Port $DOCKER_IP:${port} checked open."
done < <(image_exposed_ports_0 "$container_id")

Loading…
Cancel
Save