Indrajit Ari commited on
Commit
7ec3321
Β·
1 Parent(s): c5d9ec9

fix: resolve fastapi import and nginx permission crashes

Browse files
Files changed (3) hide show
  1. Dockerfile +14 -15
  2. nginx.conf +10 -0
  3. supervisord.conf +2 -2
Dockerfile CHANGED
@@ -1,15 +1,13 @@
1
  # ─────────────────────────────────────────────────────────────────────────────
2
  # Hugging Face Spaces β€” Docker SDK
3
  # Architecture:
4
- # supervisord manages two processes:
5
  # - Next.js standalone server on :3000
6
  # - FastAPI (uvicorn) on :8000
7
- # nginx on :7860 routes:
8
- # /api/* and /ws/* β†’ FastAPI :8000
9
- # everything else β†’ Next.js :3000
10
  # ─────────────────────────────────────────────────────────────────────────────
11
 
12
- # ── Stage 1: Build Next.js (standalone output) ─────────────────────────────
13
  FROM node:20-slim AS frontend-builder
14
 
15
  WORKDIR /build/frontend
@@ -17,16 +15,14 @@ COPY frontend/package*.json ./
17
  RUN npm ci
18
 
19
  COPY frontend/ ./
20
- # Empty API URL β†’ all /api/* and /ws/* go through nginx to FastAPI
21
  ENV NEXT_PUBLIC_API_URL=""
22
- # Enable standalone output (required for Docker; skipped in local dev)
23
  ENV BUILD_STANDALONE=1
24
  RUN npm run build
25
 
26
  # ── Stage 2: Runtime ────────────────────────────────────────────────────────
27
  FROM python:3.10-slim
28
 
29
- # System deps: ffmpeg + OpenCV libs + nginx + supervisor + Node.js runtime
30
  RUN apt-get update && apt-get install -y \
31
  ffmpeg libgl1 libglib2.0-0 \
32
  nginx supervisor curl \
@@ -36,12 +32,11 @@ RUN apt-get update && apt-get install -y \
36
 
37
  WORKDIR /app
38
 
39
- # ── Python: CPU-only torch first (layer cache) ──────────────────────────────
40
  RUN pip install --no-cache-dir \
41
  torch==2.1.2 torchvision==0.16.2 \
42
  --index-url https://download.pytorch.org/whl/cpu
43
 
44
- # ── Python: app dependencies ────────────────────────────────────────────────
45
  RUN pip install --no-cache-dir \
46
  "fastapi>=0.110.0" \
47
  "uvicorn[standard]>=0.29.0" \
@@ -61,15 +56,19 @@ COPY --from=frontend-builder /build/frontend/.next/standalone ./frontend/
61
  COPY --from=frontend-builder /build/frontend/.next/static ./frontend/.next/static
62
  COPY --from=frontend-builder /build/frontend/public ./frontend/public
63
 
64
- # ── nginx config ────────────────────────────────────────────────────────────
65
  COPY nginx.conf /etc/nginx/nginx.conf
66
-
67
- # ── supervisord config ───────────────────────────────────────────────────────
68
  COPY supervisord.conf /etc/supervisor/conf.d/app.conf
69
 
70
- # ── Directories ─────────────────────────────────────────────────────────────
 
71
  RUN mkdir -p /tmp/video_seg/uploads /tmp/video_seg/outputs \
72
- && mkdir -p /var/log/supervisor
 
 
 
 
 
73
 
74
  # HF Spaces requires port 7860
75
  EXPOSE 7860
 
1
  # ─────────────────────────────────────────────────────────────────────────────
2
  # Hugging Face Spaces β€” Docker SDK
3
  # Architecture:
4
+ # supervisord manages three processes:
5
  # - Next.js standalone server on :3000
6
  # - FastAPI (uvicorn) on :8000
7
+ # - nginx on :7860 (routing frontend + backend)
 
 
8
  # ─────────────────────────────────────────────────────────────────────────────
9
 
10
+ # ── Stage 1: Build Next.js ──────────────────────────────────────────────────
11
  FROM node:20-slim AS frontend-builder
12
 
13
  WORKDIR /build/frontend
 
15
  RUN npm ci
16
 
17
  COPY frontend/ ./
 
18
  ENV NEXT_PUBLIC_API_URL=""
 
19
  ENV BUILD_STANDALONE=1
20
  RUN npm run build
21
 
22
  # ── Stage 2: Runtime ────────────────────────────────────────────────────────
23
  FROM python:3.10-slim
24
 
25
+ # System deps
26
  RUN apt-get update && apt-get install -y \
27
  ffmpeg libgl1 libglib2.0-0 \
28
  nginx supervisor curl \
 
32
 
33
  WORKDIR /app
34
 
35
+ # ── Python deps ──────────────────────────────────────────────────────────────
36
  RUN pip install --no-cache-dir \
37
  torch==2.1.2 torchvision==0.16.2 \
38
  --index-url https://download.pytorch.org/whl/cpu
39
 
 
40
  RUN pip install --no-cache-dir \
41
  "fastapi>=0.110.0" \
42
  "uvicorn[standard]>=0.29.0" \
 
56
  COPY --from=frontend-builder /build/frontend/.next/static ./frontend/.next/static
57
  COPY --from=frontend-builder /build/frontend/public ./frontend/public
58
 
59
+ # ── Configs ──────────────────────────────────────────────────────────────────
60
  COPY nginx.conf /etc/nginx/nginx.conf
 
 
61
  COPY supervisord.conf /etc/supervisor/conf.d/app.conf
62
 
63
+ # ── Directories & Permissions ────────────────────────────────────────────────
64
+ # Ensure all runtime directories exist and are writable by any user (Hugging Face)
65
  RUN mkdir -p /tmp/video_seg/uploads /tmp/video_seg/outputs \
66
+ && mkdir -p /var/log/supervisor /var/run /var/lib/nginx /var/log/nginx \
67
+ && chmod -R 777 /tmp/video_seg \
68
+ && chmod -R 777 /var/log/supervisor \
69
+ && chmod -R 777 /var/lib/nginx \
70
+ && chmod -R 777 /var/log/nginx \
71
+ && chmod -R 777 /var/run
72
 
73
  # HF Spaces requires port 7860
74
  EXPOSE 7860
nginx.conf CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  events { worker_processes 1; }
2
 
3
  http {
@@ -5,6 +8,13 @@ http {
5
  default_type application/octet-stream;
6
  sendfile on;
7
 
 
 
 
 
 
 
 
8
  # Increase timeouts for large video uploads
9
  client_max_body_size 250m;
10
  proxy_read_timeout 300s;
 
1
+ # Run nginx in a way compatible with non-root Docker environments
2
+ # like Hugging Face Spaces.
3
+ pid /tmp/nginx.pid;
4
  events { worker_processes 1; }
5
 
6
  http {
 
8
  default_type application/octet-stream;
9
  sendfile on;
10
 
11
+ # Explicitly set paths for temporary files to /tmp
12
+ client_body_temp_path /tmp/client_temp;
13
+ proxy_temp_path /tmp/proxy_temp;
14
+ fastcgi_temp_path /tmp/fastcgi_temp;
15
+ uwsgi_temp_path /tmp/uwsgi_temp;
16
+ scgi_temp_path /tmp/scgi_temp;
17
+
18
  # Increase timeouts for large video uploads
19
  client_max_body_size 250m;
20
  proxy_read_timeout 300s;
supervisord.conf CHANGED
@@ -14,8 +14,8 @@ serverurl=unix:///tmp/supervisor.sock
14
 
15
  # ── FastAPI backend ──────────────────────────────────────────────────────────
16
  [program:fastapi]
17
- command=uvicorn backend.app_hf:app --host 127.0.0.1 --port 8000 --workers 1 --timeout-keep-alive 120
18
- directory=/app
19
  autostart=true
20
  autorestart=true
21
  stdout_logfile=/var/log/supervisor/fastapi.log
 
14
 
15
  # ── FastAPI backend ──────────────────────────────────────────────────────────
16
  [program:fastapi]
17
+ command=uvicorn app_hf:app --host 127.0.0.1 --port 8000 --workers 1 --timeout-keep-alive 120
18
+ directory=/app/backend
19
  autostart=true
20
  autorestart=true
21
  stdout_logfile=/var/log/supervisor/fastapi.log