1- # Multi-stage build for Rust backend
1+ # 极简版 Rust 后端 Dockerfile - 针对国内网络优化
22FROM rust:1.86.0-slim AS chef
33
4- # 配置国内镜像源加速
4+ # 配置阿里云镜像源
55RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
66 echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
77 echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
88
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- ENV CARGO_NET_RETRY=10
13- ENV CARGO_NET_TIMEOUT=300
14- ENV CARGO_HTTP_TIMEOUT=300
15- ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
9+ # 配置最优Rust镜像源
10+ ENV CARGO_NET_RETRY=3
11+ ENV CARGO_NET_TIMEOUT=60
12+ ENV CARGO_HTTP_TIMEOUT=60
1613
1714RUN mkdir -p ~/.cargo && \
1815 echo '[source.crates-io]' > ~/.cargo/config.toml && \
1916 echo 'replace-with = "ustc"' >> ~/.cargo/config.toml && \
20- echo '' >> ~/.cargo/config.toml && \
2117 echo '[source.ustc]' >> ~/.cargo/config.toml && \
2218 echo 'registry = "https://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config.toml && \
23- echo '' >> ~/.cargo/config.toml && \
2419 echo '[net]' >> ~/.cargo/config.toml && \
25- echo 'retry = 10' >> ~/.cargo/config.toml && \
26- echo 'timeout = 300' >> ~/.cargo/config.toml && \
27- echo '' >> ~/.cargo/config.toml && \
28- echo '[http]' >> ~/.cargo/config.toml && \
29- echo 'timeout = 300' >> ~/.cargo/config.toml
20+ echo 'retry = 3' >> ~/.cargo/config.toml && \
21+ echo 'timeout = 60' >> ~/.cargo/config.toml
3022
31- # 预先拉取 cargo-chef 源码以避免网络问题
32- RUN git clone --depth 1 https://mirrors.ustc.edu.cn/crates.io-index.git /tmp/crates-index || \
33- git clone --depth 1 https://github.com/rust-lang/crates.io-index.git /tmp/crates-index || true
23+ # 安装系统依赖
24+ RUN apt-get update && apt-get install -y --no-install-recommends \
25+ pkg-config \
26+ libssl-dev \
27+ ca-certificates \
28+ && rm -rf /var/lib/apt/lists/*
29+
30+ # 安装cargo-chef
31+ RUN cargo install cargo-chef --timeout 300
3432
35- RUN cargo install cargo-chef --timeout 600
3633WORKDIR /app
3734
3835FROM chef AS planner
@@ -42,83 +39,47 @@ RUN cargo chef prepare --recipe-path recipe.json
4239FROM chef AS builder
4340COPY --from=planner /app/recipe.json recipe.json
4441
45- # 配置国内镜像源
46- RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
47- echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
48- echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
49-
50- # 配置Rust镜像源和网络超时设置
51- ENV CARGO_NET_RETRY=10
52- ENV CARGO_NET_TIMEOUT=300
53- ENV CARGO_HTTP_TIMEOUT=300
54- ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
55-
56- RUN mkdir -p ~/.cargo && \
57- echo '[source.crates-io]' > ~/.cargo/config.toml && \
58- echo 'replace-with = "ustc"' >> ~/.cargo/config.toml && \
59- echo '' >> ~/.cargo/config.toml && \
60- echo '[source.ustc]' >> ~/.cargo/config.toml && \
61- echo 'registry = "https://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config.toml && \
62- echo '' >> ~/.cargo/config.toml && \
63- echo '[net]' >> ~/.cargo/config.toml && \
64- echo 'retry = 10' >> ~/.cargo/config.toml && \
65- echo 'timeout = 300' >> ~/.cargo/config.toml && \
66- echo '' >> ~/.cargo/config.toml && \
67- echo '[http]' >> ~/.cargo/config.toml && \
68- echo 'timeout = 300' >> ~/.cargo/config.toml
69-
70- # Install system dependencies
71- RUN apt-get update && apt-get install -y \
72- pkg-config \
73- libssl-dev \
74- ca-certificates \
75- && rm -rf /var/lib/apt/lists/*
76-
77- # Build dependencies - this layer is cached
78- RUN cargo chef cook --release --recipe-path recipe.json --timeout 600
42+ # 构建依赖
43+ RUN cargo chef cook --release --recipe-path recipe.json
7944
80- # Copy source code and build application
45+ # 复制源码并构建
8146COPY . .
82- RUN CARGO_NET_RETRY=10 CARGO_NET_TIMEOUT=300 cargo build --release --timeout 600
47+ RUN cargo build --release
8348
84- # Runtime stage
49+ # 运行时阶段
8550FROM debian:bookworm-slim AS runtime
8651
87- # 配置国内镜像源
52+ # 配置阿里云镜像源
8853RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
8954 echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
9055 echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
9156
92- # Install runtime dependencies
93- RUN apt-get update && apt-get install -y \
57+ # 安装运行时依赖
58+ RUN apt-get update && apt-get install -y --no-install-recommends \
9459 ca-certificates \
9560 libssl3 \
9661 curl \
97- && rm -rf /var/lib/apt/lists/* \
98- && apt-get clean
62+ && rm -rf /var/lib/apt/lists/*
9963
100- # Create non-root user
64+ # 创建用户
10165RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
10266
10367WORKDIR /app
10468
105- # Copy binary and config
69+ # 复制二进制文件
10670COPY --from=builder /app/target/release/backend /app/backend
10771COPY --from=builder /app/config.docker.toml /app/config.toml
10872
109- # Set ownership
11073RUN chown -R bloguser:bloguser /app
11174USER bloguser
11275
11376EXPOSE 8080
11477
115- # Health check
11678HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
11779 CMD curl -f http://localhost:8080/health || exit 1
11880
11981CMD ["./backend"]
12082
121- # Production optimized stage
12283FROM runtime AS production
12384ENV RUST_LOG=info
12485ENV RUST_BACKTRACE=0
0 commit comments