update Dockerfile

This commit is contained in:
k
2026-06-14 23:20:51 +08:00
parent 57d39d0d4b
commit fa1700d9e8
+14 -6
View File
@@ -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,6 +35,12 @@ 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