File size: 2,189 Bytes
f8670cd
 
1322216
f8670cd
 
 
 
 
 
 
 
 
 
 
 
 
 
1322216
b66e905
 
 
 
1322216
b66e905
1322216
b66e905
1322216
f8670cd
4b66647
1322216
4b66647
 
 
 
 
 
 
 
 
 
008e2f6
 
 
 
4b66647
b66e905
f8670cd
 
 
 
1322216
4b66647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ee465
4b66647
 
1322216
b66e905
 
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
# ── Stage 1: Frontend Builder ─────────────────────────────────
FROM node:20-slim AS frontend-builder

WORKDIR /src/dashboard

# Install dependencies
COPY dashboard/package*.json ./
RUN npm install

# Copy source and build (vite.config.ts outputs to ../static/dashboard)
COPY dashboard/ .
RUN npm run build

# ── Stage 2: Python Builder ───────────────────────────────────
FROM python:3.11-slim AS python-builder

WORKDIR /build-python

# Environment setup
WORKDIR /app
RUN python -m venv /app/venv
ENV PATH="/app/venv/bin:$PATH"

# Install dependencies in venv
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# ── Stage 3: Production ───────────────────────────────────────
FROM python:3.11-slim AS production

# Security: run as non-root user
RUN useradd --create-home --shell /bin/bash appuser

WORKDIR /app

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

# Use virtualenv binaries
ENV PATH="/app/venv/bin:$PATH"
ENV PYTHONPATH="/app"

# Copy virtualenv from builder
COPY --from=python-builder /app/venv /app/venv

# Copy dashboard build from frontend-builder
# (Vite config builds to ../static/dashboard relative to /src/dashboard)
COPY --chown=appuser:appuser --from=frontend-builder /src/static/dashboard /app/static/dashboard

# Copy application code
COPY --chown=appuser:appuser . .

# Create data directory for SQLite DB
RUN mkdir -p /app/data && chown appuser:appuser /app/data

# Switch to non-root user
USER appuser

# Use venv python
ENV PATH="/app/venv/bin:$PATH"
ENV PYTHONPATH="/app"
ENV APP_PORT=7860

EXPOSE 7860

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

# Run the application using python -m for maximum portability
CMD ["python", "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]