peihsin0715
commited on
Commit
·
e8997ce
1
Parent(s):
7c447a5
Update Dockerfile configuration
Browse files- Dockerfile +40 -11
Dockerfile
CHANGED
|
@@ -6,35 +6,64 @@ RUN npm ci
|
|
| 6 |
COPY frontend/ ./
|
| 7 |
RUN npm run build
|
| 8 |
|
| 9 |
-
# ---------- Backend build ----------
|
| 10 |
FROM python:3.11-slim AS be
|
| 11 |
ENV PIP_NO_CACHE_DIR=1 PYTHONUNBUFFERED=1 PIP_PREFER_BINARY=1
|
| 12 |
WORKDIR /app
|
|
|
|
|
|
|
| 13 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 14 |
-
build-essential gcc g++ gfortran make pkg-config \
|
| 15 |
-
libopenblas-dev liblapack-dev git \
|
| 16 |
-
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
RUN python -m pip install --upgrade pip setuptools wheel
|
|
|
|
|
|
|
| 18 |
RUN pip install --index-url https://download.pytorch.org/whl/cpu "torch==2.3.1"
|
|
|
|
|
|
|
| 19 |
COPY backend/requirements.txt ./backend/requirements.txt
|
| 20 |
RUN sed -i 's/^[Tt]orch[[:space:]=<>!].*/# torch pinned separately (CPU)/' backend/requirements.txt || true
|
|
|
|
|
|
|
| 21 |
RUN pip install --only-binary=:all: blis || echo "Precompiled blis not available"
|
| 22 |
RUN pip install -r backend/requirements.txt || pip install -r backend/requirements.txt --no-deps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
COPY backend/ ./backend/
|
| 24 |
|
| 25 |
# ---------- Runtime ----------
|
| 26 |
FROM python:3.11-slim AS runtime
|
| 27 |
-
ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PORT=7860
|
|
|
|
|
|
|
| 28 |
WORKDIR /app
|
|
|
|
|
|
|
| 29 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 30 |
-
nginx supervisor ca-certificates \
|
| 31 |
-
libgomp1 libopenblas0 \
|
| 32 |
-
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
| 33 |
COPY --from=fe /app/frontend/dist /usr/share/nginx/html
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
COPY --from=be /app/backend /app/backend
|
| 36 |
-
RUN python -m pip install --no-cache-dir gunicorn
|
| 37 |
|
|
|
|
| 38 |
COPY nginx.conf.template /etc/nginx/nginx.conf
|
| 39 |
|
| 40 |
RUN mkdir -p /etc/supervisor/conf.d && \
|
|
@@ -52,4 +81,4 @@ stdout_logfile_maxbytes=0\nstderr_logfile_maxbytes=0\n\n\
|
|
| 52 |
> /etc/supervisor/conf.d/app.conf
|
| 53 |
|
| 54 |
EXPOSE 7860
|
| 55 |
-
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/app.conf"]
|
|
|
|
| 6 |
COPY frontend/ ./
|
| 7 |
RUN npm run build
|
| 8 |
|
| 9 |
+
# ---------- Backend build (build venv only) ----------
|
| 10 |
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 ----------
|
| 45 |
FROM python:3.11-slim AS runtime
|
| 46 |
+
ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PORT=7860 \
|
| 47 |
+
PATH="/opt/venv/bin:${PATH}"
|
| 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 && \
|
|
|
|
| 81 |
> /etc/supervisor/conf.d/app.conf
|
| 82 |
|
| 83 |
EXPOSE 7860
|
| 84 |
+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/app.conf"]
|