tinazhang128 Claude Opus 4.5 commited on
Commit
ca42e91
·
1 Parent(s): df1de91

Update Dockerfile for API + frontend architecture with nginx reverse proxy

Browse files

Rewrote Dockerfile as multi-stage build: Stage 1 builds Next.js frontend,
Stage 2 runs R plumber API + Next.js + nginx behind supervisord on port 7860.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Files changed (3) hide show
  1. Dockerfile +38 -19
  2. nginx.conf +44 -0
  3. supervisord.conf +35 -0
Dockerfile CHANGED
@@ -1,20 +1,28 @@
1
- # MCAT Shiny App Dockerfile
2
- # For deployment to Hugging Face Spaces, AWS, or any Docker-compatible platform
 
3
 
4
- # Use Ubuntu with R (stable, supports ARM64/Apple Silicon)
 
 
 
 
 
 
 
 
 
 
 
 
5
  FROM ubuntu:22.04
6
 
7
- # Prevent interactive prompts during installation
8
  ENV DEBIAN_FRONTEND=noninteractive
9
-
10
- # Set UTF-8 locale
11
  ENV LANG=C.UTF-8
12
  ENV LC_ALL=C.UTF-8
13
-
14
- # Prevent browser launch attempts
15
  ENV BROWSER=""
16
 
17
- # Install R and system dependencies
18
  RUN apt-get update && apt-get install -y \
19
  locales \
20
  r-base \
@@ -29,26 +37,37 @@ RUN apt-get update && apt-get install -y \
29
  libjpeg-dev \
30
  libharfbuzz-dev \
31
  libfribidi-dev \
 
 
 
 
 
32
  && rm -rf /var/lib/apt/lists/*
33
 
34
- # Install R packages (this may take several minutes)
35
- RUN R -e "install.packages('shiny', repos='https://cloud.r-project.org/', dependencies=TRUE)"
36
- RUN R -e "install.packages(c('dplyr', 'ggplot2'), repos='https://cloud.r-project.org/', dependencies=TRUE)"
37
  RUN R -e "install.packages('mirt', repos='https://cloud.r-project.org/', dependencies=TRUE)"
38
  RUN R -e "install.packages('mirtCAT', repos='https://cloud.r-project.org/', dependencies=TRUE)"
39
 
40
  # Create app directory
41
- RUN mkdir -p /app
42
 
43
- # Copy application files
44
- COPY app.R /app/app.R
 
 
45
  COPY Fullmodel.rds /app/Fullmodel.rds
46
 
47
- # Set working directory
 
 
 
 
 
 
 
48
  WORKDIR /app
49
 
50
- # Expose port
51
  EXPOSE 7860
52
 
53
- # Run the app.R script directly (mirtCAT handles its own Shiny server)
54
- CMD ["Rscript", "/app/app.R"]
 
1
+ # MCAT - Adaptive Mental Health Assessment
2
+ # Runs R plumber API + Next.js frontend behind nginx
3
+ # For Hugging Face Spaces (exposes port 7860)
4
 
5
+ # ---- Stage 1: Build Next.js frontend ----
6
+ FROM node:18-alpine AS frontend-build
7
+
8
+ WORKDIR /frontend
9
+ COPY frontend/package.json frontend/package-lock.json ./
10
+ RUN npm ci
11
+ COPY frontend/ ./
12
+
13
+ # API calls go to /api/* which nginx proxies to the R backend
14
+ ENV NEXT_PUBLIC_API_URL=/api
15
+ RUN npm run build
16
+
17
+ # ---- Stage 2: Runtime ----
18
  FROM ubuntu:22.04
19
 
 
20
  ENV DEBIAN_FRONTEND=noninteractive
 
 
21
  ENV LANG=C.UTF-8
22
  ENV LC_ALL=C.UTF-8
 
 
23
  ENV BROWSER=""
24
 
25
+ # Install R, Node.js, nginx, supervisor
26
  RUN apt-get update && apt-get install -y \
27
  locales \
28
  r-base \
 
37
  libjpeg-dev \
38
  libharfbuzz-dev \
39
  libfribidi-dev \
40
+ nginx \
41
+ curl \
42
+ supervisor \
43
+ && curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
44
+ && apt-get install -y nodejs \
45
  && rm -rf /var/lib/apt/lists/*
46
 
47
+ # Install R packages
48
+ RUN R -e "install.packages(c('plumber', 'jsonlite'), repos='https://cloud.r-project.org/', dependencies=TRUE)"
 
49
  RUN R -e "install.packages('mirt', repos='https://cloud.r-project.org/', dependencies=TRUE)"
50
  RUN R -e "install.packages('mirtCAT', repos='https://cloud.r-project.org/', dependencies=TRUE)"
51
 
52
  # Create app directory
53
+ RUN mkdir -p /app/frontend
54
 
55
+ # Copy R backend files
56
+ COPY api.R /app/api.R
57
+ COPY run_api.R /app/run_api.R
58
+ COPY mirtcat_helper.R /app/mirtcat_helper.R
59
  COPY Fullmodel.rds /app/Fullmodel.rds
60
 
61
+ # Copy built Next.js standalone server + static assets
62
+ COPY --from=frontend-build /frontend/.next/standalone /app/frontend/
63
+ COPY --from=frontend-build /frontend/.next/static /app/frontend/.next/static
64
+
65
+ # Copy config files
66
+ COPY nginx.conf /etc/nginx/nginx.conf
67
+ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
68
+
69
  WORKDIR /app
70
 
 
71
  EXPOSE 7860
72
 
73
+ CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
 
nginx.conf ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ worker_processes 1;
2
+ pid /tmp/nginx.pid;
3
+
4
+ events {
5
+ worker_connections 1024;
6
+ }
7
+
8
+ http {
9
+ include /etc/nginx/mime.types;
10
+ default_type application/octet-stream;
11
+
12
+ # Use /tmp for temp files (no root needed)
13
+ client_body_temp_path /tmp/nginx_body;
14
+ proxy_temp_path /tmp/nginx_proxy;
15
+ fastcgi_temp_path /tmp/nginx_fastcgi;
16
+ uwsgi_temp_path /tmp/nginx_uwsgi;
17
+ scgi_temp_path /tmp/nginx_scgi;
18
+
19
+ access_log /dev/stdout;
20
+ error_log /dev/stderr;
21
+
22
+ server {
23
+ listen 7860;
24
+
25
+ # API requests -> R plumber backend
26
+ location /api/ {
27
+ rewrite ^/api/(.*) /$1 break;
28
+ proxy_pass http://127.0.0.1:8000;
29
+ proxy_set_header Host $host;
30
+ proxy_set_header X-Real-IP $remote_addr;
31
+ proxy_read_timeout 120s;
32
+ }
33
+
34
+ # Everything else -> Next.js frontend
35
+ location / {
36
+ proxy_pass http://127.0.0.1:3000;
37
+ proxy_set_header Host $host;
38
+ proxy_set_header X-Real-IP $remote_addr;
39
+ proxy_http_version 1.1;
40
+ proxy_set_header Upgrade $http_upgrade;
41
+ proxy_set_header Connection "upgrade";
42
+ }
43
+ }
44
+ }
supervisord.conf ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [supervisord]
2
+ nodaemon=true
3
+ logfile=/dev/null
4
+ logfile_maxbytes=0
5
+ pidfile=/tmp/supervisord.pid
6
+
7
+ [program:r-api]
8
+ command=Rscript /app/run_api.R
9
+ directory=/app
10
+ autostart=true
11
+ autorestart=true
12
+ stdout_logfile=/dev/stdout
13
+ stdout_logfile_maxbytes=0
14
+ stderr_logfile=/dev/stderr
15
+ stderr_logfile_maxbytes=0
16
+
17
+ [program:nextjs]
18
+ command=node /app/frontend/server.js
19
+ directory=/app/frontend
20
+ environment=PORT=3000,HOSTNAME="0.0.0.0"
21
+ autostart=true
22
+ autorestart=true
23
+ stdout_logfile=/dev/stdout
24
+ stdout_logfile_maxbytes=0
25
+ stderr_logfile=/dev/stderr
26
+ stderr_logfile_maxbytes=0
27
+
28
+ [program:nginx]
29
+ command=nginx -g "daemon off;"
30
+ autostart=true
31
+ autorestart=true
32
+ stdout_logfile=/dev/stdout
33
+ stdout_logfile_maxbytes=0
34
+ stderr_logfile=/dev/stderr
35
+ stderr_logfile_maxbytes=0