Files
usda-vision/docs/DOCKER_COMPOSE_ENV_SETUP.md
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

168 lines
4.3 KiB
Markdown

# 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.