Adeen commited on
Commit
d1d41c8
·
1 Parent(s): bb17288

Deploy: Unified Docker setup with Nginx and Supervisor

Browse files
Files changed (4) hide show
  1. Dockerfile +56 -0
  2. nginx.conf +27 -0
  3. src/lib/api.ts +3 -1
  4. supervisord.conf +32 -0
Dockerfile ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Stage 1: Build the Next.js frontend
2
+ FROM node:20-alpine AS builder
3
+
4
+ WORKDIR /app
5
+ COPY package*.json ./
6
+ RUN npm install
7
+
8
+ COPY . .
9
+ # We need to ensure that the Next.js app knows the API URL during build/runtime
10
+ # For this Hugging Face setup with NGINX, the API is available at the same domain under /api/
11
+ ENV NEXT_PUBLIC_API_URL=/api
12
+ RUN npm run build
13
+
14
+ # Stage 2: Setup Python environment and run both applications
15
+ FROM python:3.11-slim
16
+
17
+ WORKDIR /app
18
+
19
+ # Install Node.js, NGINX, and Supervisord
20
+ RUN apt-get update && apt-get install -y \
21
+ curl \
22
+ nginx \
23
+ supervisor \
24
+ && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
25
+ && apt-get install -y nodejs \
26
+ && rm -rf /var/lib/apt/lists/*
27
+
28
+ # Install Python dependencies
29
+ COPY backend/requirements.txt /app/backend/
30
+ RUN pip install --no-cache-dir -r /app/backend/requirements.txt
31
+ RUN pip install --no-cache-dir uvicorn
32
+
33
+ # Copy built frontend from the builder stage
34
+ COPY --from=builder /app/package*.json ./
35
+ COPY --from=builder /app/.next ./.next
36
+ COPY --from=builder /app/public ./public
37
+ COPY --from=builder /app/node_modules ./node_modules
38
+
39
+ # Copy the rest of the frontend (e.g. next.config.ts) and the backend
40
+ COPY . /app
41
+
42
+ # Copy Nginx configuration
43
+ COPY nginx.conf /etc/nginx/sites-available/default
44
+
45
+ # Copy Supervisor configuration
46
+ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
47
+
48
+ # Expose the port Hugging Face Spaces expects
49
+ EXPOSE 7860
50
+
51
+ # We set the environment variables for runtime
52
+ ENV PORT=3000
53
+ ENV NEXT_PUBLIC_API_URL=/api
54
+
55
+ # Start Supervisor, which will manage Next.js, FastAPI, and NGINX
56
+ CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
nginx.conf ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ server {
2
+ listen 7860;
3
+
4
+ server_name localhost;
5
+
6
+ # Frontend (Next.js)
7
+ location / {
8
+ proxy_pass http://127.0.0.1:3000;
9
+ proxy_http_version 1.1;
10
+ proxy_set_header Upgrade $http_upgrade;
11
+ proxy_set_header Connection 'upgrade';
12
+ proxy_set_header Host $host;
13
+ proxy_cache_bypass $http_upgrade;
14
+ }
15
+
16
+ # Backend (FastAPI)
17
+ location /api/ {
18
+ # Assuming the backend API routes start with /api/ or similar.
19
+ # Adjust if necessary, but rewrites/proxying needs to handle this
20
+ proxy_pass http://127.0.0.1:8000;
21
+ proxy_http_version 1.1;
22
+ proxy_set_header Upgrade $http_upgrade;
23
+ proxy_set_header Connection 'upgrade';
24
+ proxy_set_header Host $host;
25
+ proxy_cache_bypass $http_upgrade;
26
+ }
27
+ }
src/lib/api.ts CHANGED
@@ -1,6 +1,8 @@
1
  import axios from 'axios';
2
 
3
- const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:8000';
 
 
4
 
5
  export class ApiService {
6
  static async createSession() {
 
1
  import axios from 'axios';
2
 
3
+ // When running in Docker on HF Spaces, the backend is on the same host,
4
+ // routed by Nginx under the `/api` or `/` path
5
+ const API_URL = process.env.NEXT_PUBLIC_API_URL || '';
6
 
7
  export class ApiService {
8
  static async createSession() {
supervisord.conf ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [supervisord]
2
+ nodaemon=true
3
+ user=root
4
+ logfile=/var/log/supervisord.log
5
+ pidfile=/var/run/supervisord.pid
6
+
7
+ [program:frontend]
8
+ command=npm run start
9
+ directory=/app
10
+ autostart=true
11
+ autorestart=true
12
+ stderr_logfile=/var/log/frontend.err.log
13
+ stdout_logfile=/var/log/frontend.out.log
14
+ user=root
15
+ environment=PORT="3000"
16
+
17
+ [program:backend]
18
+ command=uvicorn main:app --host 127.0.0.1 --port 8000
19
+ directory=/app/backend
20
+ autostart=true
21
+ autorestart=true
22
+ stderr_logfile=/var/log/backend.err.log
23
+ stdout_logfile=/var/log/backend.out.log
24
+ user=root
25
+
26
+ [program:nginx]
27
+ command=nginx -g "daemon off;"
28
+ autostart=true
29
+ autorestart=true
30
+ stderr_logfile=/var/log/nginx.err.log
31
+ stdout_logfile=/var/log/nginx.out.log
32
+ user=root