peihsin0715
commited on
Commit
·
cf579a5
1
Parent(s):
17c5c69
Fix sed error; use nginx conf.d drop-in; move pid/temp to /tmp
Browse files- Dockerfile +16 -14
Dockerfile
CHANGED
|
@@ -37,33 +37,35 @@ COPY backend/ ./backend/
|
|
| 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 |
-
#
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 \
|
|
@@ -71,11 +73,11 @@ RUN set -eux; \
|
|
| 71 |
else \
|
| 72 |
sed -ri '1i pid /tmp/nginx.pid;' /etc/nginx/nginx.conf; \
|
| 73 |
fi; \
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
#
|
| 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\
|
|
@@ -94,4 +96,4 @@ 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"]
|
|
|
|
| 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 |
+
# 建立可寫暫存
|
| 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 |
+
# venv + 後端
|
| 58 |
COPY --from=be /opt/venv /opt/venv
|
|
|
|
|
|
|
| 59 |
COPY --from=be /app/backend /app/backend
|
| 60 |
|
| 61 |
+
# nginx 主設定
|
| 62 |
COPY nginx.conf.template /etc/nginx/nginx.conf
|
| 63 |
|
| 64 |
+
# 放一個 http 級別的 drop-in,避免高風險 sed
|
| 65 |
+
RUN printf "client_max_body_size 100M;\nclient_body_temp_path /tmp/nginx/client_body;\nproxy_temp_path /tmp/nginx/proxy;\nfastcgi_temp_path /tmp/nginx/fastcgi;\nuwsgi_temp_path /tmp/nginx/uwsgi;\nscgi_temp_path /tmp/nginx/scgi;\n" \
|
| 66 |
+
> /etc/nginx/conf.d/temp_paths.conf
|
| 67 |
+
|
| 68 |
+
# 調整 nginx.conf:移除 user;設定 pid;在 http{} 內包含 conf.d
|
| 69 |
RUN set -eux; \
|
| 70 |
sed -ri 's/^\s*user\s+[^;]+;//g' /etc/nginx/nginx.conf || true; \
|
| 71 |
if grep -qE '^\s*pid\s+' /etc/nginx/nginx.conf; then \
|
|
|
|
| 73 |
else \
|
| 74 |
sed -ri '1i pid /tmp/nginx.pid;' /etc/nginx/nginx.conf; \
|
| 75 |
fi; \
|
| 76 |
+
if ! grep -q 'include /etc/nginx/conf.d/\*.conf;' /etc/nginx/nginx.conf; then \
|
| 77 |
+
sed -ri 's|(^\s*include\s+/etc/nginx/mime\.types;)|\1\n include /etc/nginx/conf.d/*.conf;|' /etc/nginx/nginx.conf; \
|
| 78 |
+
fi
|
| 79 |
|
| 80 |
+
# supervisor 設定:pidfile 放 /tmp;不要 user=
|
| 81 |
RUN mkdir -p /etc/supervisor/conf.d && \
|
| 82 |
printf "[program:api]\n\
|
| 83 |
command=gunicorn --workers 2 --threads 8 --timeout 0 --chdir /app/backend -b 0.0.0.0:5001 server:app\n\
|
|
|
|
| 96 |
> /etc/supervisor/conf.d/app.conf
|
| 97 |
|
| 98 |
EXPOSE 7860
|
| 99 |
+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/app.conf"]
|