Repository navigation
Expand file tree
/
Copy pathDockerfile.api
More file actions
55 lines (42 loc) · 1.8 KB
/
Copy pathDockerfile.api
File metadata and controls
55 lines (42 loc) · 1.8 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
# ============================================================
# 阶段一:Chef - Cargo 依赖缓存层
# ============================================================
FROM rust:1.93-bookworm AS chef
RUN cargo install cargo-chef
WORKDIR /app
# ============================================================
# 阶段二:Planner - 生成依赖配方
# ============================================================
FROM chef AS planner
COPY Cargo.toml Cargo.lock ./
COPY crates/ crates/
RUN cargo chef prepare --recipe-path recipe.json
# ============================================================
# 阶段三:Builder - 编译依赖,然后编译源码
# ============================================================
FROM chef AS builder
# 复制并预构建依赖(此层会被缓存)
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
# 复制源码并构建
COPY Cargo.toml Cargo.lock ./
COPY crates/ crates/
RUN cargo build --release --bin rustcloud-api --bin migrate
# ============================================================
# 阶段四:运行时 - 精简的生产镜像
# ============================================================
FROM debian:bookworm-slim AS runtime
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/*
RUN groupadd -r rustcloud && useradd -r -g rustcloud -s /sbin/nologin rustcloud
WORKDIR /app
COPY --from=builder /app/target/release/rustcloud-api /app/rustcloud-api
COPY --from=builder /app/target/release/migrate /app/migrate
RUN chown -R rustcloud:rustcloud /app
USER rustcloud
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8080/api/v1/health || exit 1
CMD ["./rustcloud-api"]