From 7a779015b0cc13b685cc8331bba37946d83c8e99 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sun, 25 Nov 2018 22:10:04 +0100 Subject: [PATCH] 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. --- bin/compose | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/bin/compose b/bin/compose index b9d6930..f12feeb 100755 --- a/bin/compose +++ b/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" </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")