File size: 2,262 Bytes
51e559a
 
 
 
 
322bb27
 
 
51e559a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322bb27
51e559a
322bb27
 
c93db8e
322bb27
 
2367cc3
322bb27
 
51e559a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e92d5c0
 
 
 
51e559a
 
 
 
 
 
e92d5c0
 
 
51e559a
 
 
 
 
 
 
2367cc3
51e559a
 
2367cc3
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
# =======================
# 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

# 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"]