Skip to content

Commit 7d105dd

Browse files
committed
build(docker): 优化Dockerfile配置并添加新版本
- 新增Dockerfile.simple-no-mirror和Dockerfile.ultimate两个版本 - 统一使用sed命令替换阿里云镜像源,提高可靠性 - 添加apt缓存清理步骤减少镜像体积 - 优化Rust镜像源配置,增加超时和重试设置
1 parent ef166a6 commit 7d105dd

4 files changed

Lines changed: 172 additions & 15 deletions

File tree

backend/Dockerfile.china

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# 极简版 Rust 后端 Dockerfile - 针对国内网络优化
22
FROM rust:1.86.0-slim AS chef
33

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
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 && \
7+
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
810

911
# 配置最优Rust镜像源
1012
ENV CARGO_NET_RETRY=3
@@ -21,7 +23,8 @@ RUN mkdir -p ~/.cargo && \
2123
echo 'timeout = 60' >> ~/.cargo/config.toml
2224

2325
# 安装系统依赖
24-
RUN apt-get update && apt-get install -y --no-install-recommends \
26+
RUN apt-get clean && rm -rf /var/lib/apt/lists/* && \
27+
apt-get update && apt-get install -y --no-install-recommends \
2528
pkg-config \
2629
libssl-dev \
2730
ca-certificates \
@@ -49,13 +52,16 @@ RUN cargo build --release
4952
# 运行时阶段
5053
FROM debian:bookworm-slim AS runtime
5154

52-
# 配置阿里云镜像源
53-
RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
54-
echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
55-
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
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 && \
58+
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
5661

5762
# 安装运行时依赖
58-
RUN apt-get update && apt-get install -y --no-install-recommends \
63+
RUN apt-get clean && rm -rf /var/lib/apt/lists/* && \
64+
apt-get update && apt-get install -y --no-install-recommends \
5965
ca-certificates \
6066
libssl3 \
6167
curl \
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 最简版 Rust 后端 Dockerfile - 跳过所有镜像优化
2+
FROM rust:1.86.0-slim AS builder
3+
4+
# 安装系统依赖
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
pkg-config \
7+
libssl-dev \
8+
ca-certificates \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
WORKDIR /app
12+
13+
# 复制所有源码
14+
COPY . .
15+
16+
# 直接构建
17+
RUN cargo build --release
18+
19+
# 运行时阶段
20+
FROM debian:bookworm-slim AS runtime
21+
22+
# 安装运行时依赖
23+
RUN apt-get update && apt-get install -y --no-install-recommends \
24+
ca-certificates \
25+
libssl3 \
26+
curl \
27+
&& rm -rf /var/lib/apt/lists/*
28+
29+
# 创建用户
30+
RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
31+
32+
WORKDIR /app
33+
34+
# 复制二进制文件
35+
COPY --from=builder /app/target/release/backend /app/backend
36+
COPY --from=builder /app/config.docker.toml /app/config.toml
37+
38+
RUN chown -R bloguser:bloguser /app
39+
USER bloguser
40+
41+
EXPOSE 8080
42+
43+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
44+
CMD curl -f http://localhost:8080/health || exit 1
45+
46+
CMD ["./backend"]
47+
48+
FROM runtime AS production
49+
ENV RUST_LOG=info
50+
ENV RUST_BACKTRACE=0

backend/Dockerfile.ultimate

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# 终极版 Rust 后端 Dockerfile - 完全使用阿里云镜像源
2+
FROM rust:1.86.0-slim AS chef
3+
4+
# 备份原始sources.list并完全替换为阿里云源
5+
RUN cp /etc/apt/sources.list /etc/apt/sources.list.backup && \
6+
echo "deb http://mirrors.aliyun.com/debian bookworm 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+
echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
9+
10+
# 配置阿里云Rust镜像源
11+
ENV CARGO_NET_RETRY=3
12+
ENV CARGO_NET_TIMEOUT=60
13+
ENV CARGO_HTTP_TIMEOUT=60
14+
ENV RUSTUP_DIST_SERVER=https://mirrors.aliyun.com/rustup
15+
ENV RUSTUP_UPDATE_ROOT=https://mirrors.aliyun.com/rustup/rustup
16+
17+
RUN mkdir -p ~/.cargo && \
18+
echo '[source.crates-io]' > ~/.cargo/config.toml && \
19+
echo 'replace-with = "aliyun"' >> ~/.cargo/config.toml && \
20+
echo '[source.aliyun]' >> ~/.cargo/config.toml && \
21+
echo 'registry = "https://code.aliyun.com/rustcc/crates.io-index.git"' >> ~/.cargo/config.toml && \
22+
echo '[net]' >> ~/.cargo/config.toml && \
23+
echo 'retry = 3' >> ~/.cargo/config.toml && \
24+
echo 'timeout = 60' >> ~/.cargo/config.toml
25+
26+
# 清理并安装系统依赖
27+
RUN rm -rf /var/lib/apt/lists/* && \
28+
apt-get clean && \
29+
apt-get update --fix-missing && \
30+
apt-get install -y --no-install-recommends \
31+
pkg-config \
32+
libssl-dev \
33+
ca-certificates \
34+
git \
35+
&& rm -rf /var/lib/apt/lists/* && \
36+
apt-get clean
37+
38+
# 安装cargo-chef
39+
RUN cargo install cargo-chef --timeout 300
40+
41+
WORKDIR /app
42+
43+
FROM chef AS planner
44+
COPY . .
45+
RUN cargo chef prepare --recipe-path recipe.json
46+
47+
FROM chef AS builder
48+
COPY --from=planner /app/recipe.json recipe.json
49+
50+
# 构建依赖
51+
RUN cargo chef cook --release --recipe-path recipe.json
52+
53+
# 复制源码并构建
54+
COPY . .
55+
RUN cargo build --release
56+
57+
# 运行时阶段
58+
FROM debian:bookworm-slim AS runtime
59+
60+
# 完全替换为阿里云源
61+
RUN cp /etc/apt/sources.list /etc/apt/sources.list.backup && \
62+
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-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
64+
echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
65+
66+
# 安装运行时依赖
67+
RUN rm -rf /var/lib/apt/lists/* && \
68+
apt-get clean && \
69+
apt-get update --fix-missing && \
70+
apt-get install -y --no-install-recommends \
71+
ca-certificates \
72+
libssl3 \
73+
curl \
74+
&& rm -rf /var/lib/apt/lists/* && \
75+
apt-get clean
76+
77+
# 创建用户
78+
RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
79+
80+
WORKDIR /app
81+
82+
# 复制二进制文件
83+
COPY --from=builder /app/target/release/backend /app/backend
84+
COPY --from=builder /app/config.docker.toml /app/config.toml
85+
86+
RUN chown -R bloguser:bloguser /app
87+
USER bloguser
88+
89+
EXPOSE 8080
90+
91+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
92+
CMD curl -f http://localhost:8080/health || exit 1
93+
94+
CMD ["./backend"]
95+
96+
FROM runtime AS production
97+
ENV RUST_LOG=info
98+
ENV RUST_BACKTRACE=0

blog-web/Dockerfile.china

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
# Stage 1: Build WASM
44
FROM rust:1.86.0-slim AS wasm-builder
55

6-
# 配置国内镜像源加速
7-
RUN 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/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
9-
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
6+
# 完全替换为阿里云镜像源
7+
RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list && \
8+
sed -i 's|http://security.debian.org|http://mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list && \
9+
echo "deb http://mirrors.aliyun.com/debian bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
10+
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
11+
echo "deb http://mirrors.aliyun.com/debian bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
1012

1113
# 配置Rust镜像源
1214
ENV RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
@@ -20,7 +22,8 @@ RUN mkdir -p ~/.cargo && \
2022
WORKDIR /usr/src/app
2123

2224
# Install system dependencies for wasm-pack
23-
RUN apt-get update && apt-get install -y \
25+
RUN apt-get clean && rm -rf /var/lib/apt/lists/* && \
26+
apt-get update && apt-get install -y \
2427
pkg-config \
2528
libssl-dev \
2629
curl \

0 commit comments

Comments
 (0)