Spaces:
Sleeping
Sleeping
| # ======================= | |
| # 1️⃣ Frontend build stage | |
| # ======================= | |
| FROM node:22-slim AS frontend-builder | |
| # Accept SPACE_HOST as build argument | |
| ARG SPACE_HOST | |
| # Install pnpm globally | |
| RUN corepack enable && corepack prepare pnpm@latest --activate | |
| # Set working directory | |
| WORKDIR /app/frontend | |
| # Copy package files first for caching | |
| COPY frontend/pnpm-lock.yaml frontend/package.json ./ | |
| # Install dependencies (prod only for frontend) | |
| RUN pnpm install --frozen-lockfile | |
| # Copy the rest of the frontend source | |
| COPY frontend/ ./ | |
| # Build frontend with SPACE_HOST as VITE_BACKEND_URL if provided | |
| ENV VITE_APP_ENV=production | |
| RUN if [ -n "$SPACE_HOST" ]; then \ | |
| echo "Building with SPACE_HOST: $SPACE_HOST"; \ | |
| VITE_BACKEND_URL="https://$SPACE_HOST" pnpm build; \ | |
| else \ | |
| echo "Building without SPACE_HOST"; \ | |
| VITE_BACKEND_URL="http://127.0.0.1:9481" \ | |
| pnpm build; \ | |
| fi | |
| # ======================= | |
| # 2️⃣ Backend build stage | |
| # ======================= | |
| FROM python:3.12-slim AS backend-builder | |
| # Install uv (fast Python package installer) | |
| RUN pip install --no-cache-dir uv | |
| # Set working directory | |
| WORKDIR /app | |
| # Copy backend requirements and install (no dev deps) | |
| COPY backend/pyproject.toml backend/uv.lock ./backend/ | |
| RUN cd backend && uv pip install --no-cache-dir --system . | |
| # Copy backend source | |
| COPY backend/ ./backend/ | |
| # Copy built frontend from stage 1 | |
| COPY --from=frontend-builder /app/frontend/dist ./frontend/dist | |
| # ======================= | |
| # 3️⃣ Production runtime | |
| # ======================= | |
| FROM python:3.12-slim | |
| # Create non-root user | |
| RUN useradd -m appuser | |
| # Create tmp_data directory and set permissions | |
| RUN mkdir -p /app/tmp_data && chown appuser:appuser /app/tmp_data | |
| WORKDIR /app | |
| # Copy installed packages and app | |
| COPY --from=backend-builder /usr/local /usr/local | |
| COPY --from=backend-builder /app /app | |
| # Ensure tmp_data directory has correct ownership after copying | |
| RUN chown -R appuser:appuser /app/tmp_data | |
| # Set frontend path for FastAPI | |
| ENV FRONTEND_PATH=/app/frontend/dist | |
| # Set HF_HUB_DISABLE_EXPERIMENTAL_WARNING to avoid warnings | |
| ENV HF_HUB_DISABLE_EXPERIMENTAL_WARNING=1 | |
| # Switch to non-root user | |
| USER appuser | |
| # Expose port (adjust if needed) | |
| EXPOSE 9481 | |
| # Run FastAPI via uvicorn | |
| CMD ["uvicorn", "backend.src.app:app", "--host", "0.0.0.0", "--port", "9481"] | |