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:
salirezav
2025-12-18 19:57:27 -05:00
parent 8cb45cbe03
commit 8f4225a62e
10 changed files with 603 additions and 17 deletions

View File

@@ -0,0 +1,167 @@
# Docker Compose Environment Setup
## Overview
Docker Compose now automatically detects the host machine's IP address and sets all necessary environment variables. No manual script execution is required!
## Quick Start
Instead of running `docker compose` directly, use the wrapper script:
```bash
./docker-compose.sh up -d
./docker-compose.sh down
./docker-compose.sh logs -f
# etc.
```
The wrapper script automatically:
1. Detects your host machine's IP address
2. Sets `HOST_SITE_URL` for Supabase auth redirects
3. Sets all `VITE_*` environment variables for React apps
4. Generates/updates the `.env` file
5. Runs docker-compose with all your arguments
## What Gets Auto-Configured
The script automatically sets these environment variables:
- `HOST_IP` - The detected host machine IP (e.g., `192.168.1.87`)
- `HOST_SITE_URL` - Full URL for Supabase auth (e.g., `http://192.168.1.87:3000`)
- `VITE_SUPABASE_URL` - Supabase API URL (e.g., `http://192.168.1.87:54321`)
- `VITE_MEDIA_API_URL` - Media API URL (e.g., `http://192.168.1.87:8090`)
- `VITE_VISION_API_URL` - Vision API URL (e.g., `http://192.168.1.87:8000`)
## Manual Override
If you need to override the auto-detected values, you can:
1. **Set environment variables before running**:
```bash
export HOST_IP=192.168.1.100
./docker-compose.sh up -d
```
2. **Edit the `.env` file** (generated by the script):
```bash
# Edit .env file
nano .env
# Then run docker compose normally
docker compose up -d
```
3. **Use docker-compose directly** (with fallback values):
```bash
# The docker-compose.yml has fallback values, so this still works
docker compose up -d
```
## Environment Files
### Root `.env` File
The root `.env` file is auto-generated by `docker-compose.sh` and contains:
```
HOST_IP=192.168.1.87
HOST_SITE_URL=http://192.168.1.87:3000
VITE_SUPABASE_URL=http://192.168.1.87:54321
VITE_MEDIA_API_URL=http://192.168.1.87:8090
VITE_VISION_API_URL=http://192.168.1.87:8000
```
This file is automatically updated each time you run `docker-compose.sh`.
### `management-dashboard-web-app/.env`
This file is optional and can contain:
- `VITE_SUPABASE_URL` - Overrides the docker-compose value
- `VITE_SUPABASE_ANON_KEY` - Required for Supabase authentication
Example:
```env
VITE_SUPABASE_URL=http://localhost:54321
VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```
## For Supabase CLI
When using Supabase CLI (not Docker Compose services), you still need to set `HOST_SITE_URL`:
```bash
# Option 1: Use the environment setup script
source ./scripts/set-host-env.sh
supabase start
# Option 2: Use the wrapper script
./scripts/supabase-with-env.sh start
# Option 3: Manual
export HOST_SITE_URL="http://$(./scripts/get-host-ip.sh):3000"
supabase start
```
## How It Works
1. **IP Detection**: `scripts/get-host-ip.sh` detects the host IP using multiple methods
2. **Variable Setting**: `docker-compose.sh` sets all environment variables
3. **Env File Generation**: Creates/updates `.env` file for docker-compose to read
4. **Docker Compose**: Reads `.env` and substitutes `${VARIABLE}` syntax in `docker-compose.yml`
## Troubleshooting
### IP Detection Fails
If the script can't detect your IP:
```bash
# Check what IP is detected
./scripts/get-host-ip.sh
# Manually set it
export HOST_IP=192.168.1.100
./docker-compose.sh up -d
```
### Services Can't Connect
Make sure:
1. The detected IP is accessible from your network
2. Firewall allows connections on the required ports
3. Services are using the correct URLs (check container logs)
### Using Different Ports
If your services run on different ports, edit `.env` after it's generated:
```bash
./docker-compose.sh config # Generates .env
nano .env # Edit ports
docker compose up -d # Use docker compose directly now
```
## Migration from Manual Setup
If you were previously setting environment variables manually:
1. **Remove manual exports** from your shell profile
2. **Use `./docker-compose.sh`** instead of `docker compose`
3. **Delete old `.env` files** if they have hardcoded values (the script will regenerate)
## Example Workflow
```bash
# 1. Start all services (auto-detects IP)
./docker-compose.sh up -d
# 2. Check logs
./docker-compose.sh logs -f web
# 3. Stop services
./docker-compose.sh down
# 4. Restart with new IP detection
./docker-compose.sh up -d
```
That's it! No more manual environment variable setup needed.

View File

@@ -0,0 +1,168 @@
# Host IP Configuration for Supabase
## Overview
The Supabase `config.toml` file uses the `site_url` setting to configure authentication redirects. Instead of hardcoding the host IP address, we now use environment variables that are automatically detected from the host machine.
## Problem
Previously, the `site_url` was hardcoded (e.g., `http://exp-dash:3000`), which:
- Doesn't work when the host IP changes
- Requires manual updates for different environments
- Is not portable across different machines
## Solution
We now use the `HOST_SITE_URL` environment variable, which is automatically detected using the host machine's IP address.
## Usage
### Automatic Detection (Recommended)
Use the provided scripts to automatically detect and set the host IP:
```bash
# Option 1: Source the environment setup script
source ./scripts/set-host-env.sh
# Then run Supabase CLI commands normally
supabase start
supabase db reset
```
Or use the wrapper script that does this automatically:
```bash
# Use the wrapper script for Supabase CLI commands
./scripts/supabase-with-env.sh start
./scripts/supabase-with-env.sh db reset
```
### Manual Configuration
If automatic detection doesn't work or you want to use a specific IP:
```bash
# Set the environment variable manually
export HOST_SITE_URL="http://192.168.1.100:3000"
# Or with a different port
export HOST_SITE_URL="http://192.168.1.100:8080"
# Then run Supabase commands
supabase start
```
### For Docker Compose
If you're using Docker Compose and need to set this for Supabase CLI commands run on the host:
```bash
# Before running docker compose or Supabase CLI
export HOST_SITE_URL="http://$(./scripts/get-host-ip.sh):3000"
docker compose up -d
```
Or add it to your shell profile (`~/.bashrc` or `~/.zshrc`):
```bash
# Add to ~/.bashrc or ~/.zshrc
export HOST_SITE_URL="http://$(/path/to/USDA-VISION/scripts/get-host-ip.sh):3000"
```
## How It Works
1. **IP Detection Script** (`scripts/get-host-ip.sh`):
- Tries multiple methods to detect the host IP
- Works both inside Docker containers and on the host machine
- Falls back to `127.0.0.1` if detection fails
2. **Environment Setup Script** (`scripts/set-host-env.sh`):
- Calls the IP detection script
- Sets `HOST_SITE_URL` environment variable
- Can be sourced to set the variable in your current shell
3. **Supabase Wrapper Script** (`scripts/supabase-with-env.sh`):
- Automatically sets `HOST_SITE_URL` before running Supabase CLI
- Transparent wrapper - just use it instead of `supabase` command
4. **Config File** (`supabase/config.toml`):
- Uses `env(HOST_SITE_URL)` to read the environment variable
- Supabase CLI automatically substitutes the value
## Detection Methods
The IP detection script tries these methods in order:
1. **Default Gateway** (for containers): Gets the gateway IP which is typically the host
2. **host.docker.internal**: Uses Docker's special DNS name (if available)
3. **Network Interface**: Checks common network interfaces (eth0, enp0s8, etc.)
4. **Hostname Resolution**: Uses `hostname -I` to get the host IP
5. **Fallback**: Returns `127.0.0.1` if all methods fail
## Troubleshooting
### IP Detection Fails
If the script can't detect the IP:
1. **Check network interfaces**:
```bash
ip addr show
# or
ifconfig
```
2. **Manually set the IP**:
```bash
export HOST_SITE_URL="http://YOUR_ACTUAL_IP:3000"
```
3. **Verify the IP is accessible**:
```bash
ping $(./scripts/get-host-ip.sh)
```
### Supabase CLI Doesn't Use the Variable
Make sure you:
1. Set `HOST_SITE_URL` before running Supabase CLI
2. Run Supabase CLI from the project root (where `supabase/` directory is)
3. Check that `config.toml` uses `env(HOST_SITE_URL)` syntax
### Port Mismatch
If your React app runs on a different port, adjust the port in the URL:
```bash
export HOST_SITE_URL="http://$(./scripts/get-host-ip.sh):8080"
```
## Example Workflow
```bash
# 1. Navigate to project root
cd /path/to/USDA-VISION
# 2. Set the environment variable (automatic detection)
source ./scripts/set-host-env.sh
# 3. Verify it's set
echo $HOST_SITE_URL
# Output: http://192.168.1.100:3000
# 4. Start Supabase
supabase start
# Or use the wrapper script (does steps 2-4 automatically)
./scripts/supabase-with-env.sh start
```
## Integration with Docker Compose
If you're running Supabase via Docker Compose (not CLI), the containers use their own networking. The `HOST_SITE_URL` is primarily for Supabase CLI commands that run on the host machine.
For container-to-container communication, use Docker service names (e.g., `http://web:8080`).
For browser-based redirects from Supabase Auth back to your React app, use the actual host IP that the browser can access.