File size: 2,647 Bytes
916e12b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
FROM python:3.10-slim

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    g++ \
    postgresql-client \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Create data directory for SQLite fallback
RUN mkdir -p /app/data

# Expose ports
# - 7860: Gradio UI (default HF Spaces port)
# - 8000: FastAPI backend
# - 8900: MCP server
EXPOSE 7860 8000 8900

# Set environment variables with defaults
ENV PYTHONPATH=/app
ENV API_PORT=8000
ENV MCP_PORT=8900
ENV BACKEND_BASE_URL=http://localhost:8000
ENV RAG_MCP_URL=http://localhost:8900/rag
ENV WEB_MCP_URL=http://localhost:8900/web
ENV ADMIN_MCP_URL=http://localhost:8900/admin

# Create startup script that runs all services
RUN echo '#!/bin/bash\n\
set -e\n\
\n\
# Function to wait for a service to be ready\n\
wait_for_service() {\n\
    local url=$1\n\
    local max_attempts=30\n\
    local attempt=0\n\
    \n\
    echo "Waiting for $url to be ready..."\n\
    while [ $attempt -lt $max_attempts ]; do\n\
        if curl -s -f "$url" > /dev/null 2>&1; then\n\
            echo "$url is ready!"\n\
            return 0\n\
        fi\n\
        attempt=$((attempt + 1))\n\
        sleep 1\n\
    done\n\
    \n\
    echo "Warning: $url did not become ready after $max_attempts attempts"\n\
    return 1\n\
}\n\
\n\
# Start MCP server in background\n\
echo "Starting MCP server on port $MCP_PORT..."\n\
python -m backend.mcp_server.server &\n\
MCP_PID=$!\n\
\n\
# Wait for MCP server to be ready\n\
sleep 5\n\
if ! kill -0 $MCP_PID 2>/dev/null; then\n\
    echo "Error: MCP server failed to start"\n\
    exit 1\n\
fi\n\
\n\
# Start FastAPI backend in background\n\
echo "Starting FastAPI backend on port $API_PORT..."\n\
uvicorn backend.api.main:app --host 0.0.0.0 --port $API_PORT &\n\
API_PID=$!\n\
\n\
# Wait for FastAPI to be ready\n\
sleep 5\n\
if ! kill -0 $API_PID 2>/dev/null; then\n\
    echo "Error: FastAPI backend failed to start"\n\
    exit 1\n\
fi\n\
\n\
# Wait for health check\n\
wait_for_service "http://localhost:$API_PORT/health" || true\n\
\n\
# Start Gradio UI (foreground - this is the main process)\n\
echo "Starting Gradio UI on port 7860..."\n\
echo "All services are running:"\n\
echo "  - MCP Server: http://localhost:$MCP_PORT"\n\
echo "  - FastAPI Backend: http://localhost:$API_PORT"\n\
echo "  - Gradio UI: http://localhost:7860"\n\
python app.py\n\
' > /app/start.sh && chmod +x /app/start.sh

# Use the startup script as entrypoint
CMD ["/app/start.sh"]