diff --git a/Dockerfile b/Dockerfile index d94180c..7511f3a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,12 @@ +# syntax=docker/dockerfile:1.7 # ===== 阶段1: 构建前端 ===== FROM node:20-slim AS frontend-builder WORKDIR /build COPY web/frontend/package.json web/frontend/package-lock.json* ./ -# 优先 npmmirror,失败时回退官方 registry -RUN npm config set registry https://registry.npmmirror.com && \ +# npm 缓存挂载:即使镜像层缓存失效,也复用已下载的包,避免每次部署都重新拉依赖。 +RUN --mount=type=cache,target=/root/.npm \ + npm config set registry https://registry.npmmirror.com && \ (npm ci || npm install) || \ (npm config set registry https://registry.npmjs.org && (npm ci || npm install)) COPY web/frontend/ ./ @@ -16,7 +18,8 @@ FROM node:20-slim AS yyb-node-builder WORKDIR /build COPY services/yyb-worker/package.json services/yyb-worker/package-lock.json ./ # 优先 npmmirror,失败时回退官方 registry,避免国内构建时依赖下载中断。 -RUN npm config set registry https://registry.npmmirror.com && \ +RUN --mount=type=cache,target=/root/.npm \ + npm config set registry https://registry.npmmirror.com && \ npm ci --omit=dev || \ (npm config set registry https://registry.npmjs.org && npm ci --omit=dev) @@ -26,7 +29,10 @@ FROM python:3.12-slim AS runtime ARG USE_CHINA_MIRRORS=true # 服务器默认使用阿里云镜像;本地构建可设 USE_CHINA_MIRRORS=false 直连官方源。 -RUN if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ +# apt 缓存挂载:包索引和 .deb 都留在 BuildKit 缓存里,不再每次重下。 +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt,sharing=locked \ + if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ cp /etc/apt/sources.list.d/debian.sources /tmp/debian.sources && \ sed -i 's/deb.debian.org/mirrors.cloud.aliyuncs.com/g' /etc/apt/sources.list.d/debian.sources && \ (apt-get update -o APT::Update::Error-Mode=any || \ @@ -41,11 +47,11 @@ RUN if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ libgl1 \ libglib2.0-0 \ nodejs \ - fonts-wqy-microhei \ - && rm -rf /var/lib/apt/lists/* + fonts-wqy-microhei # 安装 uv:服务器走阿里云镜像,本地直连官方 PyPI。 -RUN if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ +RUN --mount=type=cache,target=/root/.cache/pip \ + if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ pip install -i https://mirrors.cloud.aliyuncs.com/pypi/simple/ uv || \ pip install -i https://mirrors.aliyun.com/pypi/simple/ uv || \ pip install uv; \ @@ -57,7 +63,8 @@ WORKDIR /app # 先复制依赖文件,利用 Docker 缓存层 COPY pyproject.toml uv.lock* README.md alembic.ini ./ -RUN uv venv /app/.venv && \ +RUN --mount=type=cache,target=/root/.cache/uv \ + uv venv /app/.venv && \ if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ (UV_INDEX_URL=https://mirrors.cloud.aliyuncs.com/pypi/simple/ uv pip install -e . || \ UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv pip install -e . || \ @@ -108,7 +115,8 @@ FROM runtime AS test ARG USE_CHINA_MIRRORS=true COPY tests/ ./tests/ COPY conftest.py ./ -RUN if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ +RUN --mount=type=cache,target=/root/.cache/uv \ + if [ "$USE_CHINA_MIRRORS" = "true" ]; then \ (UV_INDEX_URL=https://mirrors.cloud.aliyuncs.com/pypi/simple/ uv sync --frozen --group dev || \ UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv sync --frozen --group dev || \ uv sync --frozen --group dev); \