Skip to content

Commit 8ef0752

Browse files
committed
feat(deploy): 优化国内部署流程并添加极速构建模式
- 将Rust镜像源从清华切换为中科大以提高稳定性 - 添加网络超时和重试配置避免构建失败 - 新增Dockerfile.fast极速构建模式,使用预编译工具 - 在部署脚本中添加极速构建选项 - 优化Docker镜像加速配置和健康检查
1 parent a6d8c61 commit 8ef0752

3 files changed

Lines changed: 268 additions & 73 deletions

File tree

backend/Dockerfile.china

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@ RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free n
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.tuna.tsinghua.edu.cn/rustup
11-
ENV RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup
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
16+
1217
RUN mkdir -p ~/.cargo && \
1318
echo '[source.crates-io]' > ~/.cargo/config.toml && \
14-
echo 'replace-with = "tuna"' >> ~/.cargo/config.toml && \
15-
echo '[source.tuna]' >> ~/.cargo/config.toml && \
16-
echo 'registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"' >> ~/.cargo/config.toml
17-
18-
RUN cargo install cargo-chef
19+
echo 'replace-with = "ustc"' >> ~/.cargo/config.toml && \
20+
echo '' >> ~/.cargo/config.toml && \
21+
echo '[source.ustc]' >> ~/.cargo/config.toml && \
22+
echo 'registry = "https://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config.toml && \
23+
echo '' >> ~/.cargo/config.toml && \
24+
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
30+
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
34+
35+
RUN cargo install cargo-chef --timeout 600
1936
WORKDIR /app
2037

2138
FROM chef AS planner
@@ -30,12 +47,25 @@ RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free n
3047
echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
3148
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
3249

33-
# 配置Rust镜像源
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+
3456
RUN mkdir -p ~/.cargo && \
3557
echo '[source.crates-io]' > ~/.cargo/config.toml && \
36-
echo 'replace-with = "tuna"' >> ~/.cargo/config.toml && \
37-
echo '[source.tuna]' >> ~/.cargo/config.toml && \
38-
echo 'registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"' >> ~/.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
3969

4070
# Install system dependencies
4171
RUN apt-get update && apt-get install -y \
@@ -45,11 +75,11 @@ RUN apt-get update && apt-get install -y \
4575
&& rm -rf /var/lib/apt/lists/*
4676

4777
# Build dependencies - this layer is cached
48-
RUN cargo chef cook --release --recipe-path recipe.json
78+
RUN cargo chef cook --release --recipe-path recipe.json --timeout 600
4979

5080
# Copy source code and build application
5181
COPY . .
52-
RUN cargo build --release
82+
RUN CARGO_NET_RETRY=10 CARGO_NET_TIMEOUT=300 cargo build --release --timeout 600
5383

5484
# Runtime stage
5585
FROM debian:bookworm-slim AS runtime

backend/Dockerfile.fast

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# 极速版 Rust 后端 Dockerfile - 使用预编译工具和最优镜像源
2+
FROM rust:1.86.0-slim AS chef
3+
4+
# 配置阿里云镜像源(最稳定)
5+
RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
6+
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-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+
ENV CARGO_NET_RETRY=3
13+
ENV CARGO_NET_TIMEOUT=60
14+
ENV CARGO_HTTP_TIMEOUT=60
15+
ENV CARGO_HTTP_MULTIPLEXING=false
16+
17+
# 创建完整的cargo配置
18+
RUN mkdir -p ~/.cargo && \
19+
cat > ~/.cargo/config.toml << 'EOF'
20+
[source.crates-io]
21+
replace-with = "rsproxy"
22+
23+
[source.rsproxy]
24+
registry = "https://rsproxy.cn/crates.io-index"
25+
26+
[source.rsproxy-sparse]
27+
registry = "sparse+https://rsproxy.cn/index/"
28+
29+
[registries.rsproxy]
30+
index = "https://rsproxy.cn/crates.io-index"
31+
32+
[net]
33+
retry = 3
34+
timeout = 60
35+
36+
[http]
37+
timeout = 60
38+
multiplexing = false
39+
EOF
40+
41+
# 跳过cargo-chef安装,直接使用预编译二进制
42+
RUN apt-get update && apt-get install -y --no-install-recommends \
43+
wget \
44+
ca-certificates \
45+
&& rm -rf /var/lib/apt/lists/*
46+
47+
# 下载预编译的cargo-chef
48+
RUN wget -O /usr/local/bin/cargo-chef https://github.com/LukeMathWalker/cargo-chef/releases/download/v0.1.67/cargo-chef-x86_64-unknown-linux-musl || \
49+
cargo install cargo-chef --version 0.1.67
50+
RUN chmod +x /usr/local/bin/cargo-chef
51+
52+
WORKDIR /app
53+
54+
FROM chef AS planner
55+
COPY . .
56+
RUN cargo chef prepare --recipe-path recipe.json
57+
58+
FROM chef AS builder
59+
COPY --from=planner /app/recipe.json recipe.json
60+
61+
# 配置镜像源
62+
RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
63+
echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
64+
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
65+
66+
# 复制cargo配置
67+
RUN mkdir -p ~/.cargo && \
68+
cat > ~/.cargo/config.toml << 'EOF'
69+
[source.crates-io]
70+
replace-with = "rsproxy"
71+
72+
[source.rsproxy]
73+
registry = "https://rsproxy.cn/crates.io-index"
74+
75+
[source.rsproxy-sparse]
76+
registry = "sparse+https://rsproxy.cn/index/"
77+
78+
[registries.rsproxy]
79+
index = "https://rsproxy.cn/crates.io-index"
80+
81+
[net]
82+
retry = 3
83+
timeout = 60
84+
85+
[http]
86+
timeout = 60
87+
multiplexing = false
88+
EOF
89+
90+
# 快速安装系统依赖
91+
RUN apt-get update && apt-get install -y --no-install-recommends \
92+
pkg-config \
93+
libssl-dev \
94+
ca-certificates \
95+
&& rm -rf /var/lib/apt/lists/*
96+
97+
# 构建依赖(缓存层)
98+
RUN cargo chef cook --release --recipe-path recipe.json
99+
100+
# 复制源码并构建应用
101+
COPY . .
102+
RUN cargo build --release
103+
104+
# 运行时阶段
105+
FROM debian:bookworm-slim AS runtime
106+
107+
# 配置阿里云镜像源
108+
RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
109+
echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
110+
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
111+
112+
# 安装运行时依赖
113+
RUN apt-get update && apt-get install -y --no-install-recommends \
114+
ca-certificates \
115+
libssl3 \
116+
curl \
117+
&& rm -rf /var/lib/apt/lists/*
118+
119+
# 创建非root用户
120+
RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
121+
122+
WORKDIR /app
123+
124+
# 复制二进制文件和配置
125+
COPY --from=builder /app/target/release/backend /app/backend
126+
COPY --from=builder /app/config.docker.toml /app/config.toml
127+
128+
# 设置所有权
129+
RUN chown -R bloguser:bloguser /app
130+
USER bloguser
131+
132+
EXPOSE 8080
133+
134+
# 健康检查
135+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
136+
CMD curl -f http://localhost:8080/health || exit 1
137+
138+
CMD ["./backend"]
139+
140+
# 生产优化阶段
141+
FROM runtime AS production
142+
ENV RUST_LOG=info
143+
ENV RUST_BACKTRACE=0

0 commit comments

Comments
 (0)