1+ # Multi-stage build for Rust backend with multiple China mirror options
2+ FROM rust:1.86.0-slim AS chef
3+
4+ # 配置多个国内镜像源选择 - 使用最稳定的镜像源
5+ RUN echo "deb http://mirrors.163.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
6+ echo "deb http://mirrors.163.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
7+ echo "deb http://mirrors.163.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
8+
9+ # 配置Rust镜像源 - 使用中科大源(更稳定)
10+ ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
11+ ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
12+ RUN mkdir -p ~/.cargo && \
13+ echo '[source.crates-io]' > ~/.cargo/config.toml && \
14+ echo 'replace-with = "ustc"' >> ~/.cargo/config.toml && \
15+ echo '[source.ustc]' >> ~/.cargo/config.toml && \
16+ echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config.toml
17+
18+ # 设置apt选项以避免交互和证书问题
19+ ENV DEBIAN_FRONTEND=noninteractive
20+ RUN echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99no-check-valid-until && \
21+ echo 'Acquire::http::Pipeline-Depth "0";' >> /etc/apt/apt.conf.d/99no-check-valid-until && \
22+ echo 'Acquire::http::No-Cache "true";' >> /etc/apt/apt.conf.d/99no-check-valid-until && \
23+ echo 'Acquire::BrokenProxy "true";' >> /etc/apt/apt.conf.d/99no-check-valid-until
24+
25+ RUN cargo install cargo-chef
26+ WORKDIR /app
27+
28+ FROM chef AS planner
29+ COPY . .
30+ RUN cargo chef prepare --recipe-path recipe.json
31+
32+ FROM chef AS builder
33+ COPY --from=planner /app/recipe.json recipe.json
34+
35+ # 配置网易镜像源
36+ RUN echo "deb http://mirrors.163.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
37+ echo "deb http://mirrors.163.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
38+ echo "deb http://mirrors.163.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
39+
40+ # 配置Rust镜像源
41+ RUN mkdir -p ~/.cargo && \
42+ echo '[source.crates-io]' > ~/.cargo/config.toml && \
43+ echo 'replace-with = "ustc"' >> ~/.cargo/config.toml && \
44+ echo '[source.ustc]' >> ~/.cargo/config.toml && \
45+ echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config.toml
46+
47+ # Install system dependencies with retry mechanism
48+ RUN apt-get clean && \
49+ rm -rf /var/lib/apt/lists/* && \
50+ apt-get update --fix-missing -o Acquire::CompressionTypes::Order::=gz && \
51+ apt-get install -y --no-install-recommends \
52+ pkg-config \
53+ libssl-dev \
54+ ca-certificates \
55+ build-essential \
56+ && rm -rf /var/lib/apt/lists/* \
57+ && apt-get clean
58+
59+ # Build dependencies - this layer is cached
60+ RUN cargo chef cook --release --recipe-path recipe.json
61+
62+ # Copy source code and build application
63+ COPY . .
64+ RUN cargo build --release
65+
66+ # Runtime stage
67+ FROM debian:bookworm-slim AS runtime
68+
69+ # 配置网易镜像源(运行时)
70+ RUN echo "deb http://mirrors.163.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
71+ echo "deb http://mirrors.163.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
72+ echo "deb http://mirrors.163.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
73+
74+ # 设置apt选项
75+ ENV DEBIAN_FRONTEND=noninteractive
76+ RUN echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99no-check-valid-until && \
77+ echo 'Acquire::http::Pipeline-Depth "0";' >> /etc/apt/apt.conf.d/99no-check-valid-until && \
78+ echo 'Acquire::http::No-Cache "true";' >> /etc/apt/apt.conf.d/99no-check-valid-until
79+
80+ # Install runtime dependencies
81+ RUN apt-get clean && \
82+ rm -rf /var/lib/apt/lists/* && \
83+ apt-get update --fix-missing && \
84+ apt-get install -y --no-install-recommends \
85+ ca-certificates \
86+ libssl3 \
87+ curl \
88+ && rm -rf /var/lib/apt/lists/* \
89+ && apt-get clean
90+
91+ # Create non-root user
92+ RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
93+
94+ WORKDIR /app
95+
96+ # Copy binary and config
97+ COPY --from=builder /app/target/release/backend /app/backend
98+ COPY --from=builder /app/config.docker.toml /app/config.toml
99+
100+ # Set ownership
101+ RUN chown -R bloguser:bloguser /app
102+ USER bloguser
103+
104+ EXPOSE 8080
105+
106+ # Health check
107+ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
108+ CMD curl -f http://localhost:8080/health || exit 1
109+
110+ CMD ["./backend"]
111+
112+ # Production optimized stage
113+ FROM runtime AS production
114+ ENV RUST_LOG=info
115+ ENV RUST_BACKTRACE=0
0 commit comments