Spaces:
Running
Running
Fix: Copy frontend build before project files to prevent overwrite
Browse files- Dockerfile +46 -2
Dockerfile
CHANGED
|
@@ -1,40 +1,84 @@
|
|
| 1 |
# Stage 1: Build the React Frontend
|
|
|
|
| 2 |
FROM node:20-alpine AS build-step
|
| 3 |
|
|
|
|
|
|
|
| 4 |
WORKDIR /app/frontend
|
|
|
|
| 5 |
COPY frontend/package*.json ./
|
|
|
|
| 6 |
RUN npm install --ignore-scripts
|
| 7 |
|
|
|
|
|
|
|
| 8 |
COPY frontend/ ./
|
|
|
|
| 9 |
RUN npm run build
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Stage 2: Serve with Python FastAPI
|
|
|
|
| 12 |
FROM python:3.10-slim
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# Set up a new user named "user" with user ID 1000 (Required by Hugging Face Spaces)
|
|
|
|
| 15 |
RUN useradd -m -u 1000 user
|
|
|
|
| 16 |
USER user
|
|
|
|
| 17 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 18 |
|
|
|
|
|
|
|
| 19 |
WORKDIR /home/user/app
|
| 20 |
|
|
|
|
|
|
|
| 21 |
# Copy requirement files and install Python dependencies
|
|
|
|
| 22 |
COPY --chown=user:user requirements.txt ./
|
|
|
|
| 23 |
# We install torch CPU version to save space/memory if GPU is unavailable, though HF Spaces gives regular instances depending on config
|
|
|
|
| 24 |
RUN pip install --no-cache-dir --upgrade pip
|
|
|
|
| 25 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Copy the entire project code into the container
|
|
|
|
| 28 |
COPY --chown=user:user . .
|
| 29 |
|
| 30 |
-
|
| 31 |
-
COPY --from=build-step --chown=user:user /app/frontend/dist ./frontend/dist
|
| 32 |
|
| 33 |
# Expose the standard port Hugging Face Spaces expects
|
|
|
|
| 34 |
EXPOSE 7860
|
| 35 |
|
|
|
|
|
|
|
| 36 |
# We mount the HF Spaces cache directory for transformers to avoid downloading models on every restart
|
|
|
|
| 37 |
ENV TRANSFORMERS_CACHE="/home/user/app/.cache/huggingface"
|
| 38 |
|
|
|
|
|
|
|
| 39 |
# Run the FastAPI app on port 7860
|
|
|
|
| 40 |
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
|
|
| 1 |
# Stage 1: Build the React Frontend
|
| 2 |
+
|
| 3 |
FROM node:20-alpine AS build-step
|
| 4 |
|
| 5 |
+
|
| 6 |
+
|
| 7 |
WORKDIR /app/frontend
|
| 8 |
+
|
| 9 |
COPY frontend/package*.json ./
|
| 10 |
+
|
| 11 |
RUN npm install --ignore-scripts
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
COPY frontend/ ./
|
| 16 |
+
|
| 17 |
RUN npm run build
|
| 18 |
|
| 19 |
+
|
| 20 |
+
|
| 21 |
# Stage 2: Serve with Python FastAPI
|
| 22 |
+
|
| 23 |
FROM python:3.10-slim
|
| 24 |
|
| 25 |
+
|
| 26 |
+
|
| 27 |
# Set up a new user named "user" with user ID 1000 (Required by Hugging Face Spaces)
|
| 28 |
+
|
| 29 |
RUN useradd -m -u 1000 user
|
| 30 |
+
|
| 31 |
USER user
|
| 32 |
+
|
| 33 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 34 |
|
| 35 |
+
|
| 36 |
+
|
| 37 |
WORKDIR /home/user/app
|
| 38 |
|
| 39 |
+
|
| 40 |
+
|
| 41 |
# Copy requirement files and install Python dependencies
|
| 42 |
+
|
| 43 |
COPY --chown=user:user requirements.txt ./
|
| 44 |
+
|
| 45 |
# We install torch CPU version to save space/memory if GPU is unavailable, though HF Spaces gives regular instances depending on config
|
| 46 |
+
|
| 47 |
RUN pip install --no-cache-dir --upgrade pip
|
| 48 |
+
|
| 49 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 50 |
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Copy the compiled React frontend from the build stage into the backend's reach
|
| 54 |
+
|
| 55 |
+
# THIS MUST COME BEFORE "COPY . ." to avoid being overwritten
|
| 56 |
+
|
| 57 |
+
RUN mkdir -p /home/user/app/frontend/dist
|
| 58 |
+
|
| 59 |
+
COPY --from=build-step --chown=user:user /app/frontend/dist/ /home/user/app/frontend/dist/
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
# Copy the entire project code into the container
|
| 64 |
+
|
| 65 |
COPY --chown=user:user . .
|
| 66 |
|
| 67 |
+
|
|
|
|
| 68 |
|
| 69 |
# Expose the standard port Hugging Face Spaces expects
|
| 70 |
+
|
| 71 |
EXPOSE 7860
|
| 72 |
|
| 73 |
+
|
| 74 |
+
|
| 75 |
# We mount the HF Spaces cache directory for transformers to avoid downloading models on every restart
|
| 76 |
+
|
| 77 |
ENV TRANSFORMERS_CACHE="/home/user/app/.cache/huggingface"
|
| 78 |
|
| 79 |
+
|
| 80 |
+
|
| 81 |
# Run the FastAPI app on port 7860
|
| 82 |
+
|
| 83 |
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 84 |
+
|