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
This commit is contained in:
86
scripts/get-host-ip.sh
Executable file
86
scripts/get-host-ip.sh
Executable file
@@ -0,0 +1,86 @@
|
||||
#!/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
|
||||
|
||||
33
scripts/set-host-env.sh
Executable file
33
scripts/set-host-env.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
# Script to set HOST_SITE_URL environment variable based on detected host IP
|
||||
# This should be sourced before running Supabase CLI commands or docker-compose
|
||||
# Usage: source ./scripts/set-host-env.sh
|
||||
|
||||
# Get the script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
# Detect host IP
|
||||
HOST_IP=$("$SCRIPT_DIR/get-host-ip.sh")
|
||||
if [ $? -ne 0 ] || [ -z "$HOST_IP" ]; then
|
||||
echo "Warning: Could not detect host IP, using localhost" >&2
|
||||
HOST_IP="127.0.0.1"
|
||||
fi
|
||||
|
||||
# Set the environment variable
|
||||
# Default port is 3000, but can be overridden
|
||||
PORT="${HOST_PORT:-3000}"
|
||||
export HOST_SITE_URL="http://${HOST_IP}:${PORT}"
|
||||
|
||||
# Only print messages if not being sourced (when run directly)
|
||||
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
|
||||
echo "Set HOST_SITE_URL=$HOST_SITE_URL" >&2
|
||||
echo "To use this in your current shell, run:" >&2
|
||||
echo " source $SCRIPT_DIR/set-host-env.sh" >&2
|
||||
echo "Or export it manually:" >&2
|
||||
echo " export HOST_SITE_URL=\"$HOST_SITE_URL\"" >&2
|
||||
else
|
||||
# Being sourced - just set the variable silently (or with a brief message)
|
||||
echo "HOST_SITE_URL set to: $HOST_SITE_URL" >&2
|
||||
fi
|
||||
|
||||
24
scripts/supabase-with-env.sh
Executable file
24
scripts/supabase-with-env.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
# Wrapper script for Supabase CLI that automatically sets HOST_SITE_URL
|
||||
# Usage: ./scripts/supabase-with-env.sh <supabase-command> [args...]
|
||||
|
||||
# Get the script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
|
||||
# Source the environment setup script
|
||||
source "$SCRIPT_DIR/set-host-env.sh"
|
||||
|
||||
# Change to project root (Supabase CLI looks for supabase/ directory)
|
||||
cd "$PROJECT_ROOT" || exit 1
|
||||
|
||||
# Check if supabase command exists
|
||||
if ! command -v supabase >/dev/null 2>&1; then
|
||||
echo "Error: Supabase CLI is not installed or not in PATH" >&2
|
||||
echo "Install it from: https://supabase.com/docs/guides/cli" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run the supabase command with all arguments
|
||||
exec supabase "$@"
|
||||
|
||||
Reference in New Issue
Block a user