-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.backend
More file actions
66 lines (47 loc) · 1.32 KB
/
Copy pathDockerfile.backend
File metadata and controls
66 lines (47 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Rust 后端 Dockerfile
# 多阶段构建,减小最终镜像大小
# 构建阶段
FROM rust:1.75-bullseye as builder
# 设置工作目录
WORKDIR /app
# 复制 Cargo 配置文件
COPY backend/Cargo.toml backend/Cargo.lock ./
# 创建虚拟 main.rs 文件以缓存依赖
RUN mkdir src && echo "fn main() {}" > src/main.rs
# 构建依赖(利用 Docker 缓存层)
RUN cargo build --release
RUN rm src/main.rs
# 复制源代码
COPY backend/src ./src
# 重新构建应用
RUN touch src/main.rs && cargo build --release
# 运行阶段
FROM debian:bullseye-slim
# 安装运行时依赖
RUN apt-get update && apt-get install -y \
ca-certificates \
libssl1.1 \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# 创建应用用户
RUN useradd -m -u 1001 appuser
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/target/release/rust_web_admin .
# 复制数据库迁移文件
COPY backend/migrations ./migrations
# 更改文件所有者
RUN chown -R appuser:appuser /app
# 切换到应用用户
USER appuser
# 暴露端口
EXPOSE 3000
# 设置环境变量
ENV RUST_LOG=info
ENV RUST_BACKTRACE=1
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# 启动应用
CMD ["./rust_web_admin"]