# ✅ Coolify Deployment - FIXED!

## Issues Fixed

### Issue 1: Port Already Allocated ✅

**Error:** `Bind for 0.0.0.0:8000 failed: port is already allocated`

**Cause:** Your `docker-compose.yml` used `ports:` which tries to bind to the host's ports. Coolify's dashboard runs on port 8000, causing a conflict.

**Fix:** Changed `ports:` to `expose:` - Coolify's Caddy reverse proxy handles routing

### Issue 2: Module/File Not Found ✅

**Backend Error:** `ModuleNotFoundError: No module named 'app'`

**Frontend Error:** `npm error enoent Could not read package.json`

**Cause:** Development volume mounts (`./backend:/app`, `./frontend:/app`) override Dockerfile COPY commands. In Coolify, these paths don't exist.

**Fix:** Removed bind mounts from docker-compose.yml - files now copied by Dockerfile

## What I Fixed

### Changed in docker-compose.yml:

**Before:**
```yaml
backend:
  ports:
    - "8000:8000"  # ❌ Binds to host, causes conflict
  volumes:
    - ./backend:/app  # ❌ Overrides Dockerfile, causes ModuleNotFoundError

frontend:
  ports:
    - "5173:5173"  # ❌ Binds to host, causes conflict
  volumes:
    - ./frontend:/app  # ❌ Overrides Dockerfile, causes package.json error
    - /app/node_modules
```

**After:**
```yaml
backend:
  expose:
    - "8000"  # ✅ Internal only, Caddy handles routing
  volumes:
    - sqlite_data:/app/data  # ✅ Keep named volume for database

frontend:
  expose:
    - "5173"  # ✅ Internal only, Caddy handles routing
  # ✅ No volumes - files copied by Dockerfile
```

## Files Created for Production Deployment

1. **frontend/Dockerfile.prod** - Production-optimized multi-stage build with Nginx
2. **frontend/nginx.conf** - Nginx config with API proxy and WebSocket support
3. **docker-compose.coolify.yml** - Full production-ready config
4. **COOLIFY_DEPLOYMENT.md** - Complete deployment guide

## Next Steps

### Option 1: Quick Deploy (Use Modified docker-compose.yml)

The main `docker-compose.yml` is now fixed. Just:

1. **Commit and push:**
   ```bash
   git add docker-compose.yml
   git commit -m "Fix Coolify port binding"
   git push
   ```

2. **In Coolify UI:**
   - Trigger a new deployment
   - It should work now!

3. **Configure environment variables in Coolify:**
   ```bash
   JWT_SECRET=your-secure-secret-key-min-32-chars
   VITE_API_URL=https://your-domain.com/api
   VITE_WS_URL=wss://your-domain.com
   ```

### Option 2: Full Production Setup (Recommended for Production)

Use the production-ready configuration:

1. **Update your repository:**
   ```bash
   git add .
   git commit -m "Add production Dockerfile and Coolify config"
   git push
   ```

2. **In Coolify UI:**
   - Set "Docker Compose File" to: `docker-compose.coolify.yml`
   - Or keep using `docker-compose.yml` (now fixed)

3. **Configure routing in Coolify:**
   - Backend service: Port 8000
   - Frontend service: Port 80 (if using Dockerfile.prod) or 5173 (if using dev)

## Testing After Deployment

Once deployed successfully:

```bash
# Test backend API
curl https://your-domain.com/docs

# Test frontend
https://your-domain.com
```

## What Each File Does

| File | Purpose | When to Use |
|------|---------|-------------|
| **docker-compose.yml** | Development + Coolify compatible | Default, now fixed |
| **docker-compose.coolify.yml** | Production optimized for Coolify | Production deployments |
| **frontend/Dockerfile.prod** | Multi-stage production build | Production (better performance) |
| **frontend/nginx.conf** | Nginx reverse proxy config | With Dockerfile.prod |

## Configuration in Coolify UI

### Environment Variables to Set:

```bash
# Required
JWT_SECRET=<generate-secure-random-32-char-string>

# Optional - Coolify can auto-configure these
VITE_API_URL=https://your-app.your-domain.com/api
VITE_WS_URL=wss://your-app.your-domain.com
```

### Port Configuration:

**Backend:**
- Internal Port: `8000`
- Public: Yes (if you want direct API access) or route via frontend proxy

**Frontend:**
- Internal Port: `5173` (dev) or `80` (prod)
- Public: Yes
- Root path: `/`

## Troubleshooting

### Still Getting Port Error?

1. Check if there's an old deployment in Coolify
2. In Coolify UI, stop/delete old deployment
3. Retry

### Build Fails?

Check logs for:
- Missing `email-validator==2.1.1` in requirements.txt ✅ (already fixed)
- Frontend build errors - check `package.json` has `build` script

### Can't Access API from Frontend?

Update frontend environment variables:
```bash
VITE_API_URL=/api  # Relative URL if on same domain
```

Or use nginx proxy (already configured in nginx.conf).

## Deployment Checklist

- ✅ Port bindings removed (expose instead)
- ✅ email-validator added to requirements.txt
- ✅ Production Dockerfile created for frontend
- ✅ Nginx config created with API proxy
- ✅ Environment variables documented
- ⏳ Commit and push changes
- ⏳ Configure Coolify environment variables
- ⏳ Deploy in Coolify
- ⏳ Test the deployment

## Quick Commands

```bash
# Commit all fixes
git add .
git commit -m "Fix Coolify deployment: remove port bindings, add production config"
git push

# Test locally (optional)
docker compose up -d
docker compose ps
docker compose logs -f

# Stop local
docker compose down
```

## Summary

✅ **Main issue FIXED:** Changed `ports:` to `expose:` in docker-compose.yml

✅ **Production ready:** Created Dockerfile.prod and nginx.conf for optimal deployment

✅ **Coolify compatible:** No more port binding conflicts

🚀 **Next:** Commit, push, and redeploy in Coolify!

---

**The deployment should work now!** The port allocation error will be resolved. 🎉
