Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
112 lines (98 loc) · 5.18 KB
/
Copy pathDockerfile
File metadata and controls
112 lines (98 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# ============================================================
# reader 统一镜像多阶段构建 (context = 仓库根)
# frontend/ React (vite + pnpm) → dist
# backend/ Rust (axum) → reader-dev 二进制, rust-embed 内嵌同一份 dist
# camoufox 验证码/登录浏览器后端 (pip 包 + Firefox 二进制, 构建期下载)
# 构建: docker build -t reader .
# ============================================================
# ---------- 阶段 1:前端构建 (React) ----------
FROM node:22-slim AS web
WORKDIR /web
COPY frontend/package.json frontend/pnpm-lock.yaml frontend/pnpm-workspace.yaml ./
RUN npm i -g pnpm@11 && pnpm fetch --frozen-lockfile
COPY frontend ./
RUN pnpm install --frozen-lockfile --offline && pnpm build
# ---------- 阶段 2:后端编译(cargo-chef 三段式: 依赖层缓存, src 改动只编本 crate) ----------
FROM rust:1.97-slim AS chef
RUN cargo install cargo-chef --locked
FROM chef AS planner
WORKDIR /app
COPY backend/Cargo.toml backend/Cargo.lock ./
COPY backend/src ./src
COPY backend/.cargo ./.cargo
# rust-embed/include_bytes 路径占位(prepare 只扫源码不编译, 真产物在 cook/build 阶段注入)
RUN mkdir -p web-ui/dist web-ui/public/fonts \
&& echo '<!doctype html><title>placeholder</title>' > web-ui/dist/index.html
RUN cargo chef prepare --recipe-path /app/recipe.json
FROM chef AS cook
WORKDIR /app
ENV RUSTFLAGS="--cfg reqwest_unstable"
COPY --from=planner /app/recipe.json /app/recipe.json
RUN cargo chef cook --release --recipe-path /app/recipe.json
FROM cook AS builder
WORKDIR /app
# 真实 Cargo.toml/lock: cargo-chef 的 recipe 把包版本占位成 0.0.1, 不拷回来的话
# 二进制里的 CARGO_PKG_VERSION(getSystemInfo 的 version 字段) 永远是 0.0.1。
# 放在 src 之前, 只改源码时这层仍走缓存。
COPY backend/Cargo.toml backend/Cargo.lock ./
COPY backend/src ./src
COPY backend/.cargo ./.cargo
# GAP 176:epub 导出内嵌中文字体(include_bytes 编译期内嵌, 路径相对 cargo 根)
COPY backend/web-ui/public/fonts ./web-ui/public/fonts
# rust-embed 编译期嵌入前端(web-ui/dist 由 web 阶段产出——本仓库不存旧前端产物)
COPY --from=web /web/dist ./web-ui/dist
RUN cargo build --release
# ---------- 阶段 3:camoufox 求解后端(pip 包 + 浏览器二进制,构建期下载) ----------
FROM python:3.13-slim AS camo
RUN pip install --no-cache-dir camoufox==0.5.4 \
&& python -m camoufox fetch
# ---------- 阶段 4:运行镜像(python:3.13-slim 基础——与 camo 同解释器版本,
# camo 的 site-packages 直拷即可被 import, 避免系统 python 版本错配) ----------
FROM python:3.13-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
tzdata \
fonts-noto-cjk \
&& rm -rf /var/lib/apt/lists/*
# camoufox 运行时系统库(Firefox 内核依赖集)+ tini
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
tini \
libnss3 libnspr4 libdbus-1-3 libatk1.0-0 libatk-bridge2.0-0 \
libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 \
libxfixes3 libxrandr2 libgbm1 libasound2 libpango-1.0-0 libcairo2 \
libgtk-3-0 \
libheif-examples \
&& ln -sf /usr/bin/tini /sbin/tini \
&& rm -rf /var/lib/apt/lists/*
COPY --from=camo /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages
COPY --from=camo /root/.cache/camoufox /root/.cache/camoufox
COPY backend/scripts/camoufox_solver.py /usr/local/bin/camoufox_solver.py
COPY backend/scripts/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENV TZ=Asia/Shanghai
ENV READER_APP_WEB_ROOT=/app/web-ui/dist
# entrypoint 常驻拉起 camoufox 求解服务(见 docker-entrypoint.sh); 此处显式指向它,
# 后端不再惰性 spawn(那条路径日志被丢弃且首次质询要等 20s 健康轮询)
ENV READER_CAMOUFOX_SCRIPT=/usr/local/bin/camoufox_solver.py
ENV READER_CAMOUFOX_URL=http://127.0.0.1:8196
# 离线词典数据(构建期下载, 不进仓库; WITH_DICT=0 可跳过)
# ECDICT 英汉(MIT) + 新华字典字词/词语/成语(pwxcoo/chinese-xinhua, MIT)
ARG WITH_DICT=1
RUN mkdir -p /app/dict && if [ "$WITH_DICT" = "1" ]; then \
curl -fsSL --retry 3 -o /app/dict/ecdict.csv https://raw.githubusercontent.com/skywind3000/ECDICT/master/ecdict.csv && \
curl -fsSL --retry 3 -o /app/dict/word.json https://raw.githubusercontent.com/pwxcoo/chinese-xinhua/master/data/word.json && \
curl -fsSL --retry 3 -o /app/dict/ci.json https://raw.githubusercontent.com/pwxcoo/chinese-xinhua/master/data/ci.json && \
curl -fsSL --retry 3 -o /app/dict/idiom.json https://raw.githubusercontent.com/pwxcoo/chinese-xinhua/master/data/idiom.json && \
ls -la /app/dict ; \
else echo "WITH_DICT=0: 跳过词典数据" ; fi
ENV READER_DICT_DIR=/app/dict
COPY --from=builder /app/target/release/reader-dev /usr/local/bin/reader-dev
# 唯一高频变化层(二进制 + dist)放末尾, 稳定层前置
COPY --from=builder /app/web-ui/dist /app/web-ui/dist
EXPOSE 8080
VOLUME ["/data"]
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
CMD ["reader-dev"]