Fix Docker IP detection

This commit is contained in:
2025-11-07 17:47:18 +00:00
parent 9541132046
commit 0d11d91e36
4 changed files with 110 additions and 4 deletions

View File

@@ -80,11 +80,15 @@ app = FastAPI(
async def startup_event():
"""Display server info on startup"""
import socket
from pathlib import Path
print("\n" + "=" * 60)
print("DiaryML - Your Private Creative Companion")
print("=" * 60)
# Check if running in Docker
in_docker = Path("/.dockerenv").exists()
# Get local IP for mobile setup
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -93,12 +97,19 @@ async def startup_event():
s.close()
print(f"\n Desktop: http://localhost:8000")
print(f" Mobile: http://{local_ip}:8000/api")
print(f"\n Enter the Mobile URL in your phone's DiaryML app")
# Only show IP if not in Docker or if it's a real local IP
if in_docker or local_ip.startswith("172.") or local_ip.startswith("10.0."):
print(f"\n For Mobile: Use start-docker.bat (Windows) or start-docker.sh (Mac/Linux)")
print(f" to see your mobile URL")
else:
print(f" Mobile: http://{local_ip}:8000/api")
print(f"\n Enter the Mobile URL in your phone's DiaryML app")
print("=" * 60 + "\n")
except Exception as e:
print(f"\n Desktop: http://localhost:8000")
print(f" Mobile: (Could not detect IP: {e})")
print(f" (Could not detect IP: {e})")
print("=" * 60 + "\n")

View File

@@ -6,7 +6,8 @@ services:
context: .
dockerfile: Dockerfile
container_name: diaryml
network_mode: host
ports:
- "8000:8000"
volumes:
# Persist database and data
- ./diary.db:/app/diary.db

50
start-docker.bat Normal file
View File

@@ -0,0 +1,50 @@
@echo off
setlocal enabledelayedexpansion
REM DiaryML Docker Startup Script for Windows
REM Detects your local IP and starts Docker container
echo ============================================================
echo DiaryML - Docker Startup
echo ============================================================
echo.
REM Detect local IP (looks for IPv4 starting with 192.168)
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /c:"IPv4 Address"') do (
set IP_TEMP=%%a
set IP_TEMP=!IP_TEMP: =!
echo !IP_TEMP! | findstr /b "192.168" >nul
if !errorlevel! == 0 (
set LOCAL_IP=!IP_TEMP!
goto :found
)
)
:found
if "%LOCAL_IP%"=="" (
echo.
echo WARNING: Could not auto-detect your local IP
echo Find it with: ipconfig
echo Look for "IPv4 Address" starting with 192.168
echo.
set /p LOCAL_IP="Enter your local IP (e.g., 192.168.1.100): "
)
echo.
echo Starting DiaryML Docker container...
docker-compose up -d
echo.
echo ============================================================
echo DiaryML is running!
echo ============================================================
echo.
echo Desktop: http://localhost:8000
echo Mobile: http://%LOCAL_IP%:8000/api
echo.
echo Enter the Mobile URL in your phone's DiaryML app
echo ============================================================
echo.
echo To stop: docker-compose down
echo To view logs: docker-compose logs -f diaryml
echo.
pause

44
start-docker.sh Executable file
View File

@@ -0,0 +1,44 @@
#!/bin/bash
# DiaryML Docker Startup Script
# Detects your local IP and starts Docker container
echo "============================================================"
echo "DiaryML - Docker Startup"
echo "============================================================"
# Detect local IP
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null)
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux
LOCAL_IP=$(ip route get 8.8.8.8 2>/dev/null | grep -oP 'src \K\S+')
fi
if [ -z "$LOCAL_IP" ]; then
echo ""
echo "⚠️ Could not auto-detect your local IP"
echo " Find it manually:"
echo " - Mac: System Preferences → Network"
echo " - Linux: ip addr show"
echo ""
read -p "Enter your local IP (e.g., 192.168.1.100): " LOCAL_IP
fi
echo ""
echo "Starting DiaryML Docker container..."
docker-compose up -d
echo ""
echo "============================================================"
echo "DiaryML is running!"
echo "============================================================"
echo ""
echo " Desktop: http://localhost:8000"
echo " Mobile: http://$LOCAL_IP:8000/api"
echo ""
echo " Enter the Mobile URL in your phone's DiaryML app"
echo "============================================================"
echo ""
echo "To stop: docker-compose down"
echo "To view logs: docker-compose logs -f diaryml"