Files
usda-vision/scripts/get-host-ip.sh
salirezav 8f4225a62e feat: Add dynamic host IP detection for Docker Compose and Supabase config
- Add docker-compose.sh wrapper script that auto-detects host IP
- Update docker-compose.yml to use environment variable substitution
- Update Supabase config.toml files to use HOST_SITE_URL and SUPABASE_API_URL env vars
- Add scripts/get-host-ip.sh for IP detection
- Add scripts/set-host-env.sh for environment setup
- Add scripts/supabase-with-env.sh wrapper for Supabase CLI
- Add documentation for Docker Compose environment setup
- Update README.md with new usage instructions
- Replace hardcoded URLs with dynamic environment variables
2025-12-18 19:57:27 -05:00

87 lines
2.8 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
}
# Main logic: Try methods in order of reliability
detect_host_ip() {
local ip=""
# 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