peihsin0715
commited on
Commit
·
8b951c1
1
Parent(s):
e8997ce
Fix supervisor user and adjust nginx config for HuggingFace
Browse files- Dockerfile +10 -14
- nginx.conf.template +5 -7
Dockerfile
CHANGED
@@ -11,34 +11,26 @@ FROM python:3.11-slim AS be
|
|
11 |
ENV PIP_NO_CACHE_DIR=1 PYTHONUNBUFFERED=1 PIP_PREFER_BINARY=1
|
12 |
WORKDIR /app
|
13 |
|
14 |
-
# 系統相依(供一些科學套件編譯/連結)
|
15 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
16 |
build-essential gcc g++ gfortran make pkg-config \
|
17 |
libopenblas-dev liblapack-dev git \
|
18 |
&& rm -rf /var/lib/apt/lists/*
|
19 |
|
20 |
-
# 建立虛擬環境並啟用
|
21 |
RUN python -m venv /opt/venv
|
22 |
ENV PATH="/opt/venv/bin:${PATH}"
|
23 |
|
24 |
-
# 升級 pip/工具
|
25 |
RUN python -m pip install --upgrade pip setuptools wheel
|
26 |
|
27 |
-
# 先裝 CPU 版 torch(避免跟 requirements 衝突)
|
28 |
RUN pip install --index-url https://download.pytorch.org/whl/cpu "torch==2.3.1"
|
29 |
|
30 |
-
# 安裝你的後端依賴(移除/忽略裡面的 torch)
|
31 |
COPY backend/requirements.txt ./backend/requirements.txt
|
32 |
RUN sed -i 's/^[Tt]orch[[:space:]=<>!].*/# torch pinned separately (CPU)/' backend/requirements.txt || true
|
33 |
|
34 |
-
# 盡量吃 wheels;若失敗再退而求其次
|
35 |
RUN pip install --only-binary=:all: blis || echo "Precompiled blis not available"
|
36 |
RUN pip install -r backend/requirements.txt || pip install -r backend/requirements.txt --no-deps
|
37 |
|
38 |
-
# 也把 gunicorn 裝進 venv(改用這個,避免 runtime 再裝)
|
39 |
RUN pip install --no-cache-dir gunicorn
|
40 |
|
41 |
-
# 可選:把後端程式先帶進來做可用性檢查(不一定需要)
|
42 |
COPY backend/ ./backend/
|
43 |
|
44 |
# ---------- Runtime ----------
|
@@ -48,24 +40,27 @@ ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PORT=7860 \
|
|
48 |
|
49 |
WORKDIR /app
|
50 |
|
51 |
-
# 輕量執行期相依
|
52 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
53 |
nginx supervisor ca-certificates \
|
54 |
libgomp1 libopenblas0 \
|
55 |
&& rm -rf /var/lib/apt/lists/*
|
56 |
|
57 |
-
# 前端靜態檔
|
58 |
COPY --from=fe /app/frontend/dist /usr/share/nginx/html
|
59 |
|
60 |
-
# 只拷「虛擬環境」,不要整個 /usr/local
|
61 |
COPY --from=be /opt/venv /opt/venv
|
62 |
|
63 |
-
# 後端程式碼
|
64 |
COPY --from=be /app/backend /app/backend
|
65 |
|
66 |
-
# nginx 設定與 supervisor
|
67 |
COPY nginx.conf.template /etc/nginx/nginx.conf
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
RUN mkdir -p /etc/supervisor/conf.d && \
|
70 |
printf "[program:api]\n\
|
71 |
command=gunicorn --workers 2 --threads 8 --timeout 0 --chdir /app/backend -b 0.0.0.0:5001 server:app\n\
|
@@ -77,7 +72,8 @@ command=nginx -g \"daemon off;\"\n\
|
|
77 |
priority=20\nautostart=true\nautorestart=true\n\
|
78 |
stdout_logfile=/dev/stdout\nstderr_logfile=/dev/stderr\n\
|
79 |
stdout_logfile_maxbytes=0\nstderr_logfile_maxbytes=0\n\n\
|
80 |
-
[supervisord]\
|
|
|
81 |
> /etc/supervisor/conf.d/app.conf
|
82 |
|
83 |
EXPOSE 7860
|
|
|
11 |
ENV PIP_NO_CACHE_DIR=1 PYTHONUNBUFFERED=1 PIP_PREFER_BINARY=1
|
12 |
WORKDIR /app
|
13 |
|
|
|
14 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
15 |
build-essential gcc g++ gfortran make pkg-config \
|
16 |
libopenblas-dev liblapack-dev git \
|
17 |
&& rm -rf /var/lib/apt/lists/*
|
18 |
|
|
|
19 |
RUN python -m venv /opt/venv
|
20 |
ENV PATH="/opt/venv/bin:${PATH}"
|
21 |
|
|
|
22 |
RUN python -m pip install --upgrade pip setuptools wheel
|
23 |
|
|
|
24 |
RUN pip install --index-url https://download.pytorch.org/whl/cpu "torch==2.3.1"
|
25 |
|
|
|
26 |
COPY backend/requirements.txt ./backend/requirements.txt
|
27 |
RUN sed -i 's/^[Tt]orch[[:space:]=<>!].*/# torch pinned separately (CPU)/' backend/requirements.txt || true
|
28 |
|
|
|
29 |
RUN pip install --only-binary=:all: blis || echo "Precompiled blis not available"
|
30 |
RUN pip install -r backend/requirements.txt || pip install -r backend/requirements.txt --no-deps
|
31 |
|
|
|
32 |
RUN pip install --no-cache-dir gunicorn
|
33 |
|
|
|
34 |
COPY backend/ ./backend/
|
35 |
|
36 |
# ---------- Runtime ----------
|
|
|
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\
|
|
|
72 |
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
|
nginx.conf.template
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
worker_processes auto;
|
2 |
events {
|
3 |
worker_connections 1024;
|
@@ -14,15 +15,13 @@ http {
|
|
14 |
server_name _;
|
15 |
root /usr/share/nginx/html;
|
16 |
index index.html;
|
17 |
-
|
18 |
-
# 前端單頁應用
|
19 |
location / {
|
20 |
try_files $uri $uri/ /index.html;
|
21 |
}
|
22 |
-
|
23 |
-
# 後端 API 反向代理
|
24 |
location /api/ {
|
25 |
-
proxy_pass http://127.0.0.
|
26 |
proxy_http_version 1.1;
|
27 |
proxy_set_header Upgrade $http_upgrade;
|
28 |
proxy_set_header Connection "upgrade";
|
@@ -34,7 +33,6 @@ http {
|
|
34 |
proxy_send_timeout 3000s;
|
35 |
proxy_read_timeout 3000s;
|
36 |
proxy_redirect off;
|
37 |
-
|
38 |
}
|
39 |
}
|
40 |
-
}
|
|
|
1 |
+
pid /tmp/nginx.pid;
|
2 |
worker_processes auto;
|
3 |
events {
|
4 |
worker_connections 1024;
|
|
|
15 |
server_name _;
|
16 |
root /usr/share/nginx/html;
|
17 |
index index.html;
|
18 |
+
|
|
|
19 |
location / {
|
20 |
try_files $uri $uri/ /index.html;
|
21 |
}
|
22 |
+
|
|
|
23 |
location /api/ {
|
24 |
+
proxy_pass http://127.0.0.2:5001/;
|
25 |
proxy_http_version 1.1;
|
26 |
proxy_set_header Upgrade $http_upgrade;
|
27 |
proxy_set_header Connection "upgrade";
|
|
|
33 |
proxy_send_timeout 3000s;
|
34 |
proxy_read_timeout 3000s;
|
35 |
proxy_redirect off;
|
|
|
36 |
}
|
37 |
}
|
38 |
+
}
|