Skip to content

Commit 4403b44

Browse files
committed
refactor(docker): 精简并优化Docker配置
重构所有Dockerfile配置,删除冗余文件并优化国内镜像设置。主要变更包括: - 删除7个冗余Dockerfile,保留优化后的国内版和最小化版本 - 统一使用阿里云镜像源和USTC Rust镜像 - 简化构建流程,移除不必要的cargo-chef阶段 - 优化健康检查和运行时配置 新增部署脚本和指南,支持1Panel环境一键部署
1 parent 7d105dd commit 4403b44

11 files changed

Lines changed: 1016 additions & 654 deletions

DEPLOYMENT-GUIDE.md

Lines changed: 461 additions & 0 deletions
Large diffs are not rendered by default.

backend/Dockerfile.china

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
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镜像源和网络超时
1211
ENV CARGO_NET_RETRY=3
1312
ENV CARGO_NET_TIMEOUT=60
1413
ENV 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-
3631
WORKDIR /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
# 复制源码并构建
4934
COPY . .
5035
RUN cargo build --release
5136

5237
# 运行时阶段
5338
FROM 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用户
7154
RUN groupadd -r bloguser && useradd -r -g bloguser bloguser
7255

7356
WORKDIR /app
7457

75-
# 复制二进制文件
58+
# 复制二进制文件和配置
7659
COPY --from=builder /app/target/release/backend /app/backend
7760
COPY --from=builder /app/config.docker.toml /app/config.toml
7861

62+
# 设置所有权
7963
RUN chown -R bloguser:bloguser /app
8064
USER bloguser
8165

8266
EXPOSE 8080
8367

68+
# 健康检查
8469
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
8570
CMD curl -f http://localhost:8080/health || exit 1
8671

8772
CMD ["./backend"]
8873

74+
# 生产环境标签
8975
FROM runtime AS production
9076
ENV RUST_LOG=info
9177
ENV RUST_BACKTRACE=0

backend/Dockerfile.china-alt

Lines changed: 0 additions & 115 deletions
This file was deleted.

backend/Dockerfile.fast

Lines changed: 0 additions & 139 deletions
This file was deleted.

0 commit comments

Comments
 (0)