peihsin0715 commited on
Commit
17c5c69
·
1 Parent(s): 8b951c1

Fix pid/temp paths for non-root: supervisord and nginx; set MPLCONFIGDIR

Browse files
Files changed (1) hide show
  1. Dockerfile +27 -10
Dockerfile CHANGED
@@ -36,31 +36,46 @@ COPY backend/ ./backend/
36
  # ---------- Runtime ----------
37
  FROM python:3.11-slim AS runtime
38
  ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PORT=7860 \
39
- PATH="/opt/venv/bin:${PATH}"
 
40
 
41
  WORKDIR /app
42
 
 
43
  RUN apt-get update && apt-get install -y --no-install-recommends \
44
  nginx supervisor ca-certificates \
45
  libgomp1 libopenblas0 \
46
  && rm -rf /var/lib/apt/lists/*
47
 
 
 
 
 
 
48
  COPY --from=fe /app/frontend/dist /usr/share/nginx/html
49
 
 
50
  COPY --from=be /opt/venv /opt/venv
51
 
 
52
  COPY --from=be /app/backend /app/backend
53
 
 
54
  COPY nginx.conf.template /etc/nginx/nginx.conf
55
 
 
56
  RUN set -eux; \
57
- sed -ri 's/^\s*user\s+[^;]+;//g' /etc/nginx/nginx.conf || true; \
58
- if grep -qE '^\s*pid\s+' /etc/nginx/nginx.conf; then \
59
- sed -ri 's|^\s*pid\s+[^;]+;|pid /tmp/nginx.pid;|' /etc/nginx/nginx.conf; \
60
- else \
61
- sed -ri '1i pid /tmp/nginx.pid;' /etc/nginx/nginx.conf; \
62
- fi
63
-
 
 
 
 
64
  RUN mkdir -p /etc/supervisor/conf.d && \
65
  printf "[program:api]\n\
66
  command=gunicorn --workers 2 --threads 8 --timeout 0 --chdir /app/backend -b 0.0.0.0:5001 server:app\n\
@@ -73,8 +88,10 @@ priority=20\nautostart=true\nautorestart=true\n\
73
  stdout_logfile=/dev/stdout\nstderr_logfile=/dev/stderr\n\
74
  stdout_logfile_maxbytes=0\nstderr_logfile_maxbytes=0\n\n\
75
  [supervisord]\n\
76
- logfile=/dev/stdout\nlogfile_maxbytes=0\nnodaemon=true\n" \
 
 
77
  > /etc/supervisor/conf.d/app.conf
78
 
79
  EXPOSE 7860
80
- CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/app.conf"]
 
36
  # ---------- Runtime ----------
37
  FROM python:3.11-slim AS runtime
38
  ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PORT=7860 \
39
+ PATH="/opt/venv/bin:${PATH}" \
40
+ MPLCONFIGDIR=/tmp/matplotlib
41
 
42
  WORKDIR /app
43
 
44
+ # 輕量執行期相依
45
  RUN apt-get update && apt-get install -y --no-install-recommends \
46
  nginx supervisor ca-certificates \
47
  libgomp1 libopenblas0 \
48
  && rm -rf /var/lib/apt/lists/*
49
 
50
+ # 建立可寫的暫存與 pid 目錄
51
+ RUN mkdir -p /tmp/nginx/client_body /tmp/nginx/proxy /tmp/nginx/fastcgi /tmp/nginx/uwsgi /tmp/nginx/scgi \
52
+ /tmp/matplotlib
53
+
54
+ # 前端靜態檔
55
  COPY --from=fe /app/frontend/dist /usr/share/nginx/html
56
 
57
+ # 只拷「虛擬環境」
58
  COPY --from=be /opt/venv /opt/venv
59
 
60
+ # 後端程式碼
61
  COPY --from=be /app/backend /app/backend
62
 
63
+ # nginx 設定
64
  COPY nginx.conf.template /etc/nginx/nginx.conf
65
 
66
+ # 調整 nginx:移除 user 指令、把 pid 與 temp 目錄轉到 /tmp
67
  RUN set -eux; \
68
+ sed -ri 's/^\s*user\s+[^;]+;//g' /etc/nginx/nginx.conf || true; \
69
+ if grep -qE '^\s*pid\s+' /etc/nginx/nginx.conf; then \
70
+ sed -ri 's|^\s*pid\s+[^;]+;|pid /tmp/nginx.pid;|' /etc/nginx/nginx.conf; \
71
+ else \
72
+ sed -ri '1i pid /tmp/nginx.pid;' /etc/nginx/nginx.conf; \
73
+ fi; \
74
+ # 若沒有 temp 路徑,就在 http {} 內加入;有的話改成 /tmp
75
+ sed -ri 's|client_max_body_size .*;||g' /etc/nginx/nginx.conf || true; \
76
+ sed -ri '/http\s*{.*/a \ client_max_body_size 100M;\n client_body_temp_path /tmp/nginx/client_body;\n proxy_temp_path /tmp/nginx/proxy;\n fastcgi_temp_path /tmp/nginx/fastcgi;\n uwsgi_temp_path /tmp/nginx/uwsgi;\n scgi_temp_path /tmp/nginx/scgi;' /etc/nginx/nginx.conf
77
+
78
+ # 產生 supervisor 設定:把 pidfile 放 /tmp,且不使用任何 user=
79
  RUN mkdir -p /etc/supervisor/conf.d && \
80
  printf "[program:api]\n\
81
  command=gunicorn --workers 2 --threads 8 --timeout 0 --chdir /app/backend -b 0.0.0.0:5001 server:app\n\
 
88
  stdout_logfile=/dev/stdout\nstderr_logfile=/dev/stderr\n\
89
  stdout_logfile_maxbytes=0\nstderr_logfile_maxbytes=0\n\n\
90
  [supervisord]\n\
91
+ logfile=/dev/stdout\nlogfile_maxbytes=0\n\
92
+ pidfile=/tmp/supervisord.pid\n\
93
+ nodaemon=true\n" \
94
  > /etc/supervisor/conf.d/app.conf
95
 
96
  EXPOSE 7860
97
+ CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/app.conf"]