1- # 极简版 Rust 后端 Dockerfile - 针对国内网络优化
2- FROM rust:1.86.0-slim AS chef
1+ # 国内网络优化版 Rust 后端 Dockerfile
2+ FROM rust:1.86.0-slim AS builder
33
4- # 完全替换为阿里云镜像源
5- RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list && \
6- sed -i 's|http://security.debian.org|http://mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list && \
4+ # 完全替换为阿里云APT镜像源
5+ RUN rm -rf /etc/apt/sources.list && \
76 echo "deb http://mirrors.aliyun.com/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
8- echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
9- echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
7+ echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
8+ echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
109
11- # 配置最优Rust镜像源
10+ # 配置Rust镜像源和网络超时
1211ENV CARGO_NET_RETRY=3
1312ENV CARGO_NET_TIMEOUT=60
1413ENV CARGO_HTTP_TIMEOUT=60
@@ -23,69 +22,56 @@ RUN mkdir -p ~/.cargo && \
2322 echo 'timeout = 60' >> ~/.cargo/config.toml
2423
2524# 安装系统依赖
26- RUN apt-get clean && rm -rf /var/lib/apt/lists/* && \
27- apt-get update && apt-get install -y --no-install-recommends \
25+ RUN apt-get update && apt-get install -y --no-install-recommends \
2826 pkg-config \
2927 libssl-dev \
3028 ca-certificates \
3129 && rm -rf /var/lib/apt/lists/*
3230
33- # 安装cargo-chef
34- RUN cargo install cargo-chef --timeout 300
35-
3631WORKDIR /app
3732
38- FROM chef AS planner
39- COPY . .
40- RUN cargo chef prepare --recipe-path recipe.json
41-
42- FROM chef AS builder
43- COPY --from=planner /app/recipe.json recipe.json
44-
45- # 构建依赖
46- RUN cargo chef cook --release --recipe-path recipe.json
47-
4833# 复制源码并构建
4934COPY . .
5035RUN cargo build --release
5136
5237# 运行时阶段
5338FROM debian:bookworm-slim AS runtime
5439
55- # 完全替换为阿里云镜像源
56- RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list && \
57- sed -i 's|http://security.debian.org|http://mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list && \
40+ # 配置阿里云APT镜像源
41+ RUN rm -rf /etc/apt/sources.list && \
5842 echo "deb http://mirrors.aliyun.com/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
59- echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
60- echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
43+ echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
44+ echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
6145
6246# 安装运行时依赖
63- RUN apt-get clean && rm -rf /var/lib/apt/lists/* && \
64- apt-get update && apt-get install -y --no-install-recommends \
47+ RUN apt-get update && apt-get install -y --no-install-recommends \
6548 ca-certificates \
6649 libssl3 \
6750 curl \
6851 && rm -rf /var/lib/apt/lists/*
6952
70- # 创建用户
53+ # 创建非root用户
7154RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
7255
7356WORKDIR /app
7457
75- # 复制二进制文件
58+ # 复制二进制文件和配置
7659COPY --from=builder /app/target/release/backend /app/backend
7760COPY --from=builder /app/config.docker.toml /app/config.toml
7861
62+ # 设置所有权
7963RUN chown -R bloguser:bloguser /app
8064USER bloguser
8165
8266EXPOSE 8080
8367
68+ # 健康检查
8469HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
8570 CMD curl -f http://localhost:8080/health || exit 1
8671
8772CMD ["./backend"]
8873
74+ # 生产环境标签
8975FROM runtime AS production
9076ENV RUST_LOG=info
9177ENV RUST_BACKTRACE=0
0 commit comments