File size: 2,045 Bytes
dc3879e
 
 
69be42f
 
 
 
dc3879e
 
 
 
 
 
 
69be42f
1c7da91
dc3879e
 
69be42f
1c7da91
dc3879e
 
 
 
 
 
 
 
8327a4f
 
dc3879e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69be42f
1c7da91
 
 
 
 
 
 
 
 
 
69be42f
dc3879e
 
69be42f
8327a4f
 
dc3879e
 
 
8327a4f
69be42f
8327a4f
 
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
# Multi-stage Dockerfile for FastAPI Backend
# Stage 1: Builder stage - Install dependencies with UV
FROM python:3.13-slim AS builder

# Set working directory
WORKDIR /app

# Install system dependencies and UV
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        gcc \
        libpq-dev \
        && rm -rf /var/lib/apt/lists/* && \
    pip install --no-cache-dir uv

# Copy dependency files
COPY pyproject.toml ./
COPY uv.lock ./

# Install dependencies to a temporary location
RUN uv sync --no-dev --no-editable

# Stage 2: Production stage - Copy dependencies and run application
FROM python:3.13-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PATH="/app/.venv/bin:$PATH" \
    PORT=7860

# Create non-root user
RUN groupadd -r appuser -g 1000 && \
    useradd -r -u 1000 -g appuser -s /sbin/nologin -d /app -m appuser

# Set working directory
WORKDIR /app

# Install runtime dependencies only
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        libpq5 \
        curl \
        && rm -rf /var/lib/apt/lists/*

# Copy virtual environment from builder
COPY --from=builder --chown=appuser:appuser /app/.venv /app/.venv

# Copy application code (flat structure)
COPY --chown=appuser:appuser api/ api/
COPY --chown=appuser:appuser core/ core/
COPY --chown=appuser:appuser models/ models/
COPY --chown=appuser:appuser ai_agent/ ai_agent/
COPY --chown=appuser:appuser services/ services/
COPY --chown=appuser:appuser ws_manager/ ws_manager/
COPY --chown=appuser:appuser mcp_server/ mcp_server/
COPY --chown=appuser:appuser main.py .
COPY --chown=appuser:appuser uvicorn_config.py .

# Switch to non-root user
USER appuser

# Expose port 7860 (Hugging Face Spaces default)
EXPOSE 7860

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
    CMD curl -f http://localhost:7860/health || exit 1

# Run the application with uvicorn on port 7860
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]