diff --git a/test.sh b/test.sh index d84b83e..0c6732f 100755 --- a/test.sh +++ b/test.sh @@ -193,6 +193,12 @@ function start_test_server() { log_info "Container IP address: ${CONTAINER_IP}" fi + # Try to get the Docker host IP (gateway) for Docker-in-Docker + HOST_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}' "${CONTAINER_NAME}") + if [ -n "${HOST_IP}" ]; then + log_info "Docker host IP (gateway): ${HOST_IP}" + fi + # Check if container is still running if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then log_error "Container stopped unexpectedly. Logs:" @@ -213,22 +219,31 @@ function start_test_server() { local attempt=0 while [ $attempt -lt $max_attempts ]; do - # Try container IP first (for Docker-in-Docker), then localhost with mapped port + # Try multiple connection methods for different environments local http_code="000" + local test_urls="" - # Try direct container IP on port 80 (works in Docker-in-Docker) + # Try direct container IP on port 80 (might work in some Docker setups) if [ "${CONTAINER_IP}" != "localhost" ]; then http_code=$(curl -s --max-time 2 -o /dev/null -w "%{http_code}" "http://${CONTAINER_IP}:80/" 2>/dev/null || echo "000") + test_urls="${CONTAINER_IP}:80" fi - # If that didn't work, try localhost with mapped port + # If that didn't work, try host IP with mapped port (Docker-in-Docker) + if [ "$http_code" = "000" ] && [ -n "${HOST_IP}" ]; then + http_code=$(curl -s --max-time 2 -o /dev/null -w "%{http_code}" "http://${HOST_IP}:${TEST_PORT}/" 2>/dev/null || echo "000") + test_urls="${test_urls}, host:${HOST_IP}:${TEST_PORT}" + fi + + # If that didn't work, try localhost with mapped port (local Docker) if [ "$http_code" = "000" ]; then http_code=$(curl -s --max-time 2 -o /dev/null -w "%{http_code}" "http://localhost:${TEST_PORT}/" 2>/dev/null || echo "000") + test_urls="${test_urls}, localhost:${TEST_PORT}" fi # Debug: Show HTTP code and container status on first few attempts if [ $attempt -lt 3 ]; then - log_info "Health check attempt $((attempt + 1)): HTTP code ${http_code} (IP: ${CONTAINER_IP}:80, localhost:${TEST_PORT})" + log_info "Health check attempt $((attempt + 1)): HTTP code ${http_code} (tried: ${test_urls})" # Check if container is actually running if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then log_info "Container is running, checking logs..." @@ -240,18 +255,16 @@ function start_test_server() { if [ "$http_code" = "200" ] || [ "$http_code" = "204" ] || [ "$http_code" = "404" ]; then log_info "Server is ready! (HTTP ${http_code})" - # Set which connection method works for later use - if [ "${CONTAINER_IP}" != "localhost" ]; then - # Test which one actually worked - if curl -s --max-time 1 -o /dev/null "http://${CONTAINER_IP}:80/" 2>/dev/null; then - export SOS_TEST_HOST="${CONTAINER_IP}:80" - log_info "Using container IP: ${SOS_TEST_HOST}" - else - export SOS_TEST_HOST="localhost:${TEST_PORT}" - log_info "Using localhost: ${SOS_TEST_HOST}" - fi + # Determine which connection method worked + if curl -s --max-time 1 -o /dev/null "http://${CONTAINER_IP}:80/" 2>/dev/null; then + export SOS_TEST_HOST="${CONTAINER_IP}:80" + log_info "Using container IP: ${SOS_TEST_HOST}" + elif [ -n "${HOST_IP}" ] && curl -s --max-time 1 -o /dev/null "http://${HOST_IP}:${TEST_PORT}/" 2>/dev/null; then + export SOS_TEST_HOST="${HOST_IP}:${TEST_PORT}" + log_info "Using host IP: ${SOS_TEST_HOST}" else export SOS_TEST_HOST="localhost:${TEST_PORT}" + log_info "Using localhost: ${SOS_TEST_HOST}" fi return 0 fi