Files
usda-vision/scripts/get-host-ip.sh
salirezav 38a7846e7b Update .gitignore and refactor docker-compose.sh for improved host IP detection
- Added .host-ip to .gitignore to prevent tracking of host IP configurations.
- Enhanced docker-compose.sh to prioritize HOST_IP environment variable, fallback to .host-ip file, and finally auto-detect the host IP, improving robustness in various environments.
- Updated documentation to reflect changes in Supabase migration paths and removed references to deprecated management-dashboard-web-app directory.
2026-02-09 13:21:08 -05:00

109 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Script to detect the host machine's IP address
# This is useful for Docker containers that need to connect back to services on the host
# Method 1: Try to get IP from default gateway (works in most Docker setups)
# The default gateway in a Docker container is typically the host machine
get_ip_from_gateway() {
# Try to get the default gateway IP
if command -v ip >/dev/null 2>&1; then
ip route show default | awk '/default/ {print $3}' 2>/dev/null | head -1
elif command -v route >/dev/null 2>&1; then
route -n | awk '/^0.0.0.0/ {print $2}' 2>/dev/null | head -1
fi
}
# Method 2: Try host.docker.internal (works on Docker Desktop and with extra_hosts)
get_ip_from_host_docker_internal() {
if command -v getent >/dev/null 2>&1; then
getent hosts host.docker.internal 2>/dev/null | awk '{print $1}' | head -1
elif command -v nslookup >/dev/null 2>&1; then
nslookup host.docker.internal 2>/dev/null | grep -A1 "Name:" | grep "Address:" | awk '{print $2}' | head -1
fi
}
# Method 3: Get IP from network interface (for host machine)
get_ip_from_interface() {
# Try common network interfaces
for interface in eth0 enp0s8 enp0s3 ens33; do
if command -v ip >/dev/null 2>&1; then
ip addr show "$interface" 2>/dev/null | grep -oP 'inet \K[\d.]+' | head -1
elif command -v ifconfig >/dev/null 2>&1; then
ifconfig "$interface" 2>/dev/null | grep -oP 'inet \K[\d.]+' | head -1
fi
done
}
# Method 4: Get IP from hostname (if hostname resolves to an IP)
get_ip_from_hostname() {
# Try to resolve the hostname
if command -v hostname >/dev/null 2>&1; then
hostname -I 2>/dev/null | awk '{print $1}'
fi
}
# Method 5: Prefer 100.x.x.x (e.g. Tailscale) when present on the host
get_ip_prefer_100() {
local ip=""
if command -v ip >/dev/null 2>&1; then
# Third column is ADDR/CIDR (e.g. 100.93.40.84/32); extract IPv4 and prefer 100.x
ip=$(ip -br addr show 2>/dev/null | awk '{print $3}' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | grep -E '^100\.' | head -1)
fi
if [ -z "$ip" ] && command -v hostname >/dev/null 2>&1; then
ip=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^100\.' | head -1)
fi
echo "$ip"
}
# Main logic: Try methods in order of reliability
detect_host_ip() {
local ip=""
# When on the host (not in Docker), prefer 100.x.x.x (Tailscale) so the dashboard is reachable via that IP
if [ ! -f /.dockerenv ] && [ -z "${DOCKER_CONTAINER:-}" ]; then
ip=$(get_ip_prefer_100)
if [ -n "$ip" ] && [ "$ip" != "127.0.0.1" ]; then
echo "$ip"
return 0
fi
fi
# If running inside Docker, try gateway method first
if [ -f /.dockerenv ] || [ -n "${DOCKER_CONTAINER:-}" ]; then
ip=$(get_ip_from_gateway)
if [ -n "$ip" ] && [ "$ip" != "127.0.0.1" ]; then
echo "$ip"
return 0
fi
# Try host.docker.internal
ip=$(get_ip_from_host_docker_internal)
if [ -n "$ip" ] && [ "$ip" != "127.0.0.1" ]; then
echo "$ip"
return 0
fi
fi
# Try interface method (works on host machine)
ip=$(get_ip_from_interface)
if [ -n "$ip" ] && [ "$ip" != "127.0.0.1" ]; then
echo "$ip"
return 0
fi
# Try hostname method
ip=$(get_ip_from_hostname)
if [ -n "$ip" ] && [ "$ip" != "127.0.0.1" ]; then
echo "$ip"
return 0
fi
# Fallback: return localhost (not ideal but better than nothing)
echo "127.0.0.1"
return 1
}
# Execute and output the IP
detect_host_ip