From fa1700d9e8836316ac47d731c609913182d1a614 Mon Sep 17 00:00:00 2001 From: k Date: Sun, 14 Jun 2026 23:20:51 +0800 Subject: [PATCH] update Dockerfile --- Dockerfile | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3de9f8a..d76f07d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,4 @@ # ── Stage 1: build ─────────────────────────────────────────────────────────── -# Compiles native extensions (ckzg, asyncpg, cryptography) so the final image -# only carries the compiled .so files, not the build toolchain. FROM python:3.11-slim AS builder RUN apt-get update && apt-get install -y --no-install-recommends \ @@ -12,19 +10,23 @@ WORKDIR /build RUN pip install --no-cache-dir --upgrade pip wheel setuptools COPY requirements.txt . -RUN pip install --no-cache-dir --prefix=/install -r requirements.txt +# 直接打包安装到标准的 site-packages 路径中,方便后面拷贝 +RUN pip install --no-cache-dir --user -r requirements.txt # ── Stage 2: runtime ───────────────────────────────────────────────────────── FROM python:3.11-slim -# Only runtime shared libs needed (libpq for asyncpg, libgomp for numpy if used) RUN apt-get update && apt-get install -y --no-install-recommends \ libpq5 curl \ && rm -rf /var/lib/apt/lists/* -# Copy compiled packages from builder -COPY --from=builder /install /usr/local +# 【核心修改】直接从 builder 的用户目录下把装好的第三方库拷贝过来 +COPY --from=builder /root/.local /root/.local + +# 【核心修改】将拷贝过来的库路径加入到环境变量中,确保 Python 100% 能找到它们 +ENV PATH=/root/.local/bin:$PATH +ENV PYTHONPATH=/root/.local/lib/python3.11/site-packages:$PYTHONPATH WORKDIR /app COPY . . @@ -33,8 +35,14 @@ RUN chmod +x entrypoint.sh # Non-root user RUN useradd -m appuser && chown -R appuser /app +# 确保新创建的 appuser 也有权限读取拷贝过来的第三方库 +RUN cp -R /root/.local /home/appuser/ && chown -R appuser:appuser /home/appuser/.local + +ENV PATH=/home/appuser/.local/bin:$PATH +ENV PYTHONPATH=/home/appuser/.local/lib/python3.11/site-packages:$PYTHONPATH + USER appuser EXPOSE 8000 -ENTRYPOINT ["./entrypoint.sh"] +ENTRYPOINT ["./entrypoint.sh"] \ No newline at end of file