yuvrajsingh6 commited on
Commit
c75d6b0
·
1 Parent(s): a5856e8

Fix: Add nginx reverse proxy for single-port deployment

Browse files
Files changed (2) hide show
  1. Dockerfile +37 -11
  2. frontend-next/src/lib/api.js +1 -1
Dockerfile CHANGED
@@ -1,36 +1,37 @@
1
- # Multi-stage build for production
2
 
3
  # Stage 1: Build Frontend
4
  FROM node:20-alpine AS frontend-builder
5
 
6
  WORKDIR /app/frontend
7
 
8
- # Copy frontend files
9
  COPY frontend-next/package*.json ./
10
  RUN npm ci
11
 
12
  COPY frontend-next/ ./
13
  RUN npm run build
14
 
15
- # Stage 2: Python Backend with Static Frontend
16
  FROM python:3.10-slim
17
 
18
  WORKDIR /app
19
 
20
- # Install Node.js for serving frontend
21
  RUN apt-get update && apt-get install -y \
 
22
  curl \
23
  && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
24
  && apt-get install -y nodejs \
25
  && rm -rf /var/lib/apt/lists/*
26
 
27
- # Copy backend files
28
  COPY backend/requirements.txt ./backend/
29
  RUN pip install --no-cache-dir -r backend/requirements.txt
30
 
 
31
  COPY backend/ ./backend/
32
 
33
- # Copy built frontend from previous stage
34
  COPY --from=frontend-builder /app/frontend/.next ./frontend/.next
35
  COPY --from=frontend-builder /app/frontend/public ./frontend/public
36
  COPY --from=frontend-builder /app/frontend/package*.json ./frontend/
@@ -38,19 +39,44 @@ COPY --from=frontend-builder /app/frontend/node_modules ./frontend/node_modules
38
  COPY --from=frontend-builder /app/frontend/next.config.mjs ./frontend/
39
  COPY --from=frontend-builder /app/frontend/src ./frontend/src
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Create startup script
42
  RUN echo '#!/bin/bash\n\
43
- cd /app/backend && uvicorn main:app --host 0.0.0.0 --port 8000 &\n\
44
- cd /app/frontend && npm start -- -p 7860 &\n\
 
45
  wait -n\n\
46
  exit $?' > /app/start.sh && chmod +x /app/start.sh
47
 
48
- # Expose port
49
  EXPOSE 7860
50
 
51
- # Set environment variables
52
  ENV PYTHONUNBUFFERED=1
53
  ENV NODE_ENV=production
54
 
55
- # Start both services
56
  CMD ["/app/start.sh"]
 
1
+ # Use a single-port approach with nginx reverse proxy
2
 
3
  # Stage 1: Build Frontend
4
  FROM node:20-alpine AS frontend-builder
5
 
6
  WORKDIR /app/frontend
7
 
 
8
  COPY frontend-next/package*.json ./
9
  RUN npm ci
10
 
11
  COPY frontend-next/ ./
12
  RUN npm run build
13
 
14
+ # Stage 2: Runtime with nginx + Python
15
  FROM python:3.10-slim
16
 
17
  WORKDIR /app
18
 
19
+ # Install nginx and Node.js
20
  RUN apt-get update && apt-get install -y \
21
+ nginx \
22
  curl \
23
  && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
24
  && apt-get install -y nodejs \
25
  && rm -rf /var/lib/apt/lists/*
26
 
27
+ # Install Python dependencies
28
  COPY backend/requirements.txt ./backend/
29
  RUN pip install --no-cache-dir -r backend/requirements.txt
30
 
31
+ # Copy backend
32
  COPY backend/ ./backend/
33
 
34
+ # Copy built frontend
35
  COPY --from=frontend-builder /app/frontend/.next ./frontend/.next
36
  COPY --from=frontend-builder /app/frontend/public ./frontend/public
37
  COPY --from=frontend-builder /app/frontend/package*.json ./frontend/
 
39
  COPY --from=frontend-builder /app/frontend/next.config.mjs ./frontend/
40
  COPY --from=frontend-builder /app/frontend/src ./frontend/src
41
 
42
+ # Configure nginx
43
+ RUN echo 'server {\n\
44
+ listen 7860;\n\
45
+ server_name _;\n\
46
+ \n\
47
+ # Frontend\n\
48
+ location / {\n\
49
+ proxy_pass http://localhost:3000;\n\
50
+ proxy_http_version 1.1;\n\
51
+ proxy_set_header Upgrade $http_upgrade;\n\
52
+ proxy_set_header Connection "upgrade";\n\
53
+ proxy_set_header Host $host;\n\
54
+ proxy_cache_bypass $http_upgrade;\n\
55
+ }\n\
56
+ \n\
57
+ # Backend API\n\
58
+ location /api/ {\n\
59
+ rewrite ^/api/(.*) /$1 break;\n\
60
+ proxy_pass http://localhost:8000;\n\
61
+ proxy_http_version 1.1;\n\
62
+ proxy_set_header Host $host;\n\
63
+ proxy_set_header X-Real-IP $remote_addr;\n\
64
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
65
+ proxy_buffering off;\n\
66
+ }\n\
67
+ }' > /etc/nginx/sites-available/default
68
+
69
  # Create startup script
70
  RUN echo '#!/bin/bash\n\
71
+ nginx\n\
72
+ cd /app/backend && uvicorn main:app --host 127.0.0.1 --port 8000 &\n\
73
+ cd /app/frontend && npm start -- -p 3000 &\n\
74
  wait -n\n\
75
  exit $?' > /app/start.sh && chmod +x /app/start.sh
76
 
 
77
  EXPOSE 7860
78
 
 
79
  ENV PYTHONUNBUFFERED=1
80
  ENV NODE_ENV=production
81
 
 
82
  CMD ["/app/start.sh"]
frontend-next/src/lib/api.js CHANGED
@@ -1,6 +1,6 @@
1
  // Basic API client for the analytical backend
2
  const API_BASE_URL = typeof window !== 'undefined' && window.location.hostname !== 'localhost'
3
- ? `${window.location.protocol}//${window.location.hostname}:8000`
4
  : 'http://localhost:8000';
5
 
6
  export const api = {
 
1
  // Basic API client for the analytical backend
2
  const API_BASE_URL = typeof window !== 'undefined' && window.location.hostname !== 'localhost'
3
+ ? '/api' // Use nginx proxy in production
4
  : 'http://localhost:8000';
5
 
6
  export const api = {