diff --git a/Dockerfile b/Dockerfile index d76f07d..3de9f8a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ # ── 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 \ @@ -10,23 +12,19 @@ WORKDIR /build RUN pip install --no-cache-dir --upgrade pip wheel setuptools COPY requirements.txt . -# 直接打包安装到标准的 site-packages 路径中,方便后面拷贝 -RUN pip install --no-cache-dir --user -r requirements.txt +RUN pip install --no-cache-dir --prefix=/install -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/* -# 【核心修改】直接从 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 +# Copy compiled packages from builder +COPY --from=builder /install /usr/local WORKDIR /app COPY . . @@ -35,14 +33,8 @@ 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"] \ No newline at end of file +ENTRYPOINT ["./entrypoint.sh"]