From 87382d7bda743eccecd0e0bfc4ee082faf66801b Mon Sep 17 00:00:00 2001 From: "tao.chen" <93983997+taochen-ct@users.noreply.github.com> Date: Tue, 4 Aug 2026 18:39:30 +0800 Subject: [PATCH] refactor: update Dockerfile --- backend/Dockerfile | 34 +--------------------------- runtime/Dockerfile | 41 ++++----------------------------- schedule/Dockerfile | 34 +--------------------------- scripts/base.Dockerfile | 50 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 103 deletions(-) create mode 100644 scripts/base.Dockerfile diff --git a/backend/Dockerfile b/backend/Dockerfile index 9e65775..ec9ccb0 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,36 +1,4 @@ -FROM python:3.12-slim-bookworm - -ENV PYTHONDONTWRITEBYTECODE=1 \ - PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/app/.venv/bin:${PATH}" \ - TZ=Asia/Shanghai -WORKDIR /app -COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv - -RUN ( \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null \ - ) || ( \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || true \ - ) - -RUN apt-get update && apt-get install -y --no-install-recommends \ - curl \ - ca-certificates \ - gcc \ - g++ \ - python3-dev \ - build-essential \ - tzdata \ - && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \ - && echo $TZ > /etc/timezone \ - && dpkg-reconfigure --frontend noninteractive tzdata \ - && rm -rf /var/lib/apt/lists/* +FROM python-base:3.12 COPY common ./common COPY backend ./backend diff --git a/runtime/Dockerfile b/runtime/Dockerfile index 891bb98..87b329b 100644 --- a/runtime/Dockerfile +++ b/runtime/Dockerfile @@ -1,50 +1,17 @@ -FROM python:3.12-slim-bookworm +FROM python-base:3.12 -ENV PYTHONDONTWRITEBYTECODE=1 \ - PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/app/.venv/bin:${PATH}" \ - TZ=Asia/Shanghai -WORKDIR /app # 从官方 Rclone 镜像直接复制 rclone 二进制文件(高效、稳定) COPY --from=rclone/rclone:latest /usr/local/bin/rclone /usr/local/bin/rclone -COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv - -RUN ( \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null \ - ) || ( \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || true \ - ) - -# 安装系统依赖(fuse3 是 rclone mount 的核心底层依赖) -RUN apt-get update && apt-get install -y --no-install-recommends \ - fuse3 \ - ca-certificates \ - curl \ - procps \ - build-essential \ - python3-dev \ - tzdata \ - && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \ - && echo $TZ > /etc/timezone \ - && dpkg-reconfigure --frontend noninteractive tzdata \ - && sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf \ - && rm -rf /var/lib/apt/lists/* - COPY pyproject.toml uv.lock ./ COPY common ./common COPY runtime ./runtime RUN UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple/" uv sync --frozen --no-dev --no-editable --package runtime -RUN mkdir -p /root/.jupyter/custom /app/.venv/share/jupyter/custom -RUN echo '#top-panel, #top-panel-wrapper { display: none !important; height: 0 !important; min-height: 0 !important; margin: 0 !important; padding: 0 !important; border: none !important; position: absolute !important; pointer-events: none !important; }' > /root/.jupyter/custom/custom.css && \ +RUN mkdir -p /root/.jupyter/custom /app/.venv/share/jupyter/custom && \ + echo '#top-panel, #top-panel-wrapper { display: none !important; height: 0 !important; min-height: 0 !important; margin: 0 !important; padding: 0 !important; border: none !important; position: absolute !important; pointer-events: none !important; }' > /root/.jupyter/custom/custom.css && \ cp /root/.jupyter/custom/custom.css /app/.venv/share/jupyter/custom/custom.css EXPOSE 8000 CMD ["uv", "run", "--frozen", "--package", "runtime", "gunicorn", "--config", "runtime/gunicorn.conf.py", "runtime.main:app"] + diff --git a/schedule/Dockerfile b/schedule/Dockerfile index f2523f3..6886e8a 100644 --- a/schedule/Dockerfile +++ b/schedule/Dockerfile @@ -1,37 +1,5 @@ -FROM python:3.12-slim-bookworm +FROM python-base:3.12 -ENV PYTHONDONTWRITEBYTECODE=1 \ - PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/app/.venv/bin:${PATH}" \ - TZ=Asia/Shanghai -WORKDIR /app - -RUN ( \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null \ - ) || ( \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ - sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ - sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || true \ - ) - -RUN apt-get update && apt-get install -y --no-install-recommends \ - curl \ - ca-certificates \ - gcc \ - g++ \ - python3-dev \ - build-essential \ - tzdata \ - && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \ - && echo $TZ > /etc/timezone \ - && dpkg-reconfigure --frontend noninteractive tzdata \ - && rm -rf /var/lib/apt/lists/* - -COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv COPY pyproject.toml uv.lock ./ COPY common ./common COPY schedule ./schedule diff --git a/scripts/base.Dockerfile b/scripts/base.Dockerfile new file mode 100644 index 0000000..043800e --- /dev/null +++ b/scripts/base.Dockerfile @@ -0,0 +1,50 @@ +# ============================================================================= +# 公共基础镜像 (python-base) +# 包含: python 运行时环境变量、apt 国内镜像源、公共系统依赖、uv 包管理器 +# 构建: docker build -f base.Dockerfile -t your-registry/python-base:3.12 . +# 推送: docker push your-registry/python-base:3.12 +# ============================================================================= +FROM python:3.12-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PYTHONPATH=/app \ + PATH="/app/.venv/bin:${PATH}" \ + TZ=Asia/Shanghai + +WORKDIR /app + +# uv 包管理器(多个业务镜像都需要) +COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv + +# 替换 apt 源为阿里云镜像(兼容 debian.sources / sources.list 两种格式) +RUN ( \ + sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ + sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ + sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ + sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null \ + ) || ( \ + sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \ + sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || \ + sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list 2>/dev/null || true \ + ) + +# 公共系统依赖 + 时区配置 +RUN apt-get update && apt-get install -y --no-install-recommends \ + curl \ + ca-certificates \ + gcc \ + g++ \ + python3-dev \ + build-essential \ + tzdata \ + && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \ + && echo $TZ > /etc/timezone \ + && dpkg-reconfigure --frontend noninteractive tzdata \ + && rm -rf /var/lib/apt/lists/* + +RUN apt-get update && apt-get install -y --no-install-recommends \ + fuse3 \ + procps \ + && sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf \ + && rm -rf /var/lib/apt/lists/* \ No newline at end of file