Skip to content

Commit f0e0aad

Browse files
authored
feat: add cargo-chef for dep cache in dockerfile (#1895)
1 parent 58e194d commit f0e0aad

14 files changed

Lines changed: 154 additions & 28 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ redis-test = "1.0.1"
141141
bincode = "2.0.1"
142142
rustls = "0.23.36"
143143
object_store = "0.13.1"
144-
qlean = "0.1.1"
144+
qlean = "0.2.2"
145145
toml = "0.9"
146146

147147
[profile.release]

extensions/rag/chat/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ name = "chat"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[lib]
7+
name = "chat"
8+
path = "src/lib.rs"
9+
10+
611
[dependencies]
712
syn = { workspace = true, features = ["full"] }
813
anyhow = { workspace = true }

extensions/rag/index/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "index"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[lib]
7+
name = "index"
8+
path = "src/lib.rs"
9+
610
[dependencies]
711
syn = { workspace = true, features = ["full"] }
812
anyhow = { workspace = true }

io-orbit/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "io-orbit"
33
version = "0.1.0"
44
edition.workspace = true
55

6+
[lib]
7+
name = "io_orbit"
8+
path = "src/lib.rs"
9+
610

711
[dependencies]
812
common = { workspace = true }

mono/Dockerfile

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,102 @@
1-
FROM rust:bookworm AS builder
1+
# ---------- planner stage ----------
2+
# This stage analyzes the Cargo workspace and produces a dependency recipe.
3+
# The recipe will change ONLY when Cargo.toml / Cargo.lock changes.
4+
FROM rust:1.93.0-bookworm AS chef
25

36
WORKDIR /opt/mega
47

5-
# build args, to specify the build type, release or debug
6-
ARG BUILD_TYPE=release
8+
# Install cargo-chef, a tool to cache Rust dependencies efficiently
9+
RUN cargo install cargo-chef --version 0.1.73
710

8-
# check arg value
9-
RUN if [ "$BUILD_TYPE" != "release" ] && [ "$BUILD_TYPE" != "debug" ]; then \
10-
echo "Invalid BUILD_TYPE: $BUILD_TYPE, must be release or debug"; \
11-
exit 1; \
12-
fi
11+
COPY Cargo.toml Cargo.lock ./
12+
COPY api-model/Cargo.toml api-model/
13+
COPY ceres/Cargo.toml ceres/
14+
COPY common/Cargo.toml common/
15+
COPY context/Cargo.toml context/
16+
COPY extensions/rag/chat/Cargo.toml extensions/rag/chat/
17+
COPY extensions/rag/index/Cargo.toml extensions/rag/index/
18+
COPY extensions/observatory/Cargo.toml extensions/observatory/
19+
COPY io-orbit/Cargo.toml io-orbit/
20+
COPY jupiter/Cargo.toml jupiter/
21+
COPY jupiter/callisto/Cargo.toml jupiter/callisto/
22+
COPY mono/Cargo.toml mono/
23+
COPY orion/Cargo.toml orion/
24+
COPY orion/audit/Cargo.toml orion/audit/
25+
COPY orion/td_util/Cargo.toml orion/td_util/
26+
COPY orion/buck/Cargo.toml orion/buck/
27+
COPY orion-server/Cargo.toml orion-server/
28+
COPY orion-server/bellatrix/Cargo.toml orion-server/bellatrix/
29+
COPY saturn/Cargo.toml saturn/
30+
COPY scorpio/Cargo.toml scorpio/
31+
COPY vault/Cargo.toml vault/
32+
33+
34+
# Generate a recipe describing the dependency graph
35+
RUN cargo chef prepare --bin mono --recipe-path recipe.json
1336

14-
# set mirror for apt
15-
# RUN echo "deb http://mirrors.ustc.edu.cn/debian bookworm main contrib non-free" > /etc/apt/sources.list && \
16-
# echo "deb http://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free" >> /etc/apt/sources.list && \
17-
# echo "deb http://mirrors.ustc.edu.cn/debian bookworm-updates main contrib non-free" >> /etc/apt/sources.list
1837

38+
# ---------- builder stage ----------
39+
# This stage builds dependencies first (cached),
40+
# then builds the actual application binary.
41+
FROM chef AS builder
42+
43+
WORKDIR /opt/mega
44+
45+
# Install system dependencies required for building
1946
RUN apt-get update && apt-get install -y \
2047
libssl-dev \
2148
ca-certificates \
2249
clang \
2350
protobuf-compiler \
2451
libprotobuf-dev
2552

26-
# copy the source code, the context must be the root of the project
53+
54+
# Copy the dependency recipe from the planner stage
55+
COPY --from=chef /opt/mega/recipe.json recipe.json
56+
57+
# Build and cache all Rust dependencies
58+
# This layer will be reused as long as dependencies do not change
59+
RUN --mount=type=cache,target=/usr/local/cargo/registry \
60+
--mount=type=cache,target=/usr/local/cargo/git \
61+
cargo chef cook --bin mono --release --recipe-path recipe.json
62+
63+
# Copy the full source code AFTER dependencies are cached
64+
# Changes to source code will not invalidate the dependency cache
2765
COPY . .
2866

29-
# build
67+
# Build arguments to control release / debug mode
68+
ARG BUILD_TYPE=release
69+
70+
# Build the mono binary
3071
RUN if [ "$BUILD_TYPE" = "release" ]; then \
3172
cargo build --release -p mono; \
3273
else \
3374
cargo build -p mono; \
3475
fi
3576

36-
# final image
77+
78+
# ---------- runtime stage ----------
79+
# This is the minimal runtime image containing only the binary and runtime deps
3780
FROM debian:bookworm-slim
3881

39-
RUN apt-get update && apt-get install -y libssl-dev ca-certificates less
82+
# Install runtime dependencies
83+
RUN apt-get update && apt-get install -y \
84+
ca-certificates \
85+
less \
86+
libssl3 \
87+
&& rm -rf /var/lib/apt/lists/*
4088

4189
ARG BUILD_TYPE=release
4290

91+
# Copy the compiled binary and startup script
4392
COPY --from=builder /opt/mega/target/$BUILD_TYPE/mono /usr/local/bin/mono
4493
COPY --from=builder /opt/mega/mono/start-mono.sh /usr/local/bin/start-mono.sh
4594

46-
RUN chmod +x /usr/local/bin/start-mono.sh
47-
RUN chmod +x /usr/local/bin/mono
95+
# Ensure binaries are executable
96+
RUN chmod +x /usr/local/bin/mono /usr/local/bin/start-mono.sh
4897

98+
# Optional volume for runtime data
4999
VOLUME /opt/mega
50100

101+
# Default startup command
51102
CMD ["bash", "-c", "/usr/local/bin/start-mono.sh"]

orion-server/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ name = "orion-server"
33
version = "0.1.0"
44
edition.workspace = true
55

6+
7+
[[bin]]
8+
name = "orion-server"
9+
path = "src/main.rs"
10+
11+
612
[dependencies]
713
orion = { workspace = true }
8-
common = { workspace = true}
14+
common = { workspace = true }
915
io-orbit = { workspace = true }
1016
api-model = { workspace = true }
1117

orion-server/Dockerfile

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
# ────── Stage 0: Chef ──────
2-
FROM rust:1.93-bookworm AS chef
3-
RUN cargo install cargo-chef
2+
FROM rust:1.93.0-bookworm AS chef
3+
RUN cargo install cargo-chef --version 0.1.73
44
WORKDIR /app
55

66
# ────── Stage 1: Planner ──────
77
FROM chef AS planner
8-
COPY . .
98

10-
WORKDIR /app
9+
COPY Cargo.toml Cargo.lock ./
10+
COPY api-model/Cargo.toml api-model/
11+
COPY ceres/Cargo.toml ceres/
12+
COPY common/Cargo.toml common/
13+
COPY context/Cargo.toml context/
14+
COPY extensions/rag/chat/Cargo.toml extensions/rag/chat/
15+
COPY extensions/rag/index/Cargo.toml extensions/rag/index/
16+
COPY extensions/observatory/Cargo.toml extensions/observatory/
17+
COPY io-orbit/Cargo.toml io-orbit/
18+
COPY jupiter/Cargo.toml jupiter/
19+
COPY jupiter/callisto/Cargo.toml jupiter/callisto/
20+
COPY mono/Cargo.toml mono/
21+
COPY orion/Cargo.toml orion/
22+
COPY orion/audit/Cargo.toml orion/audit/
23+
COPY orion/td_util/Cargo.toml orion/td_util/
24+
COPY orion/buck/Cargo.toml orion/buck/
25+
COPY orion-server/Cargo.toml orion-server/
26+
COPY orion-server/bellatrix/Cargo.toml orion-server/bellatrix/
27+
COPY saturn/Cargo.toml saturn/
28+
COPY scorpio/Cargo.toml scorpio/
29+
COPY vault/Cargo.toml vault/
30+
1131
RUN cargo chef prepare --bin orion-server --recipe-path recipe.json
1232

1333
# ────── Stage 2: Builder ──────
@@ -17,22 +37,29 @@ RUN apt-get update && \
1737
apt-get install -y --no-install-recommends libclang-dev && \
1838
rm -rf /var/lib/apt/lists/*
1939

20-
WORKDIR /app
2140

2241
COPY --from=planner /app/recipe.json recipe.json
2342

24-
RUN cargo chef cook --bin orion-server --release --recipe-path recipe.json
43+
# -- Step 1: cook dependencies with cache
44+
RUN --mount=type=cache,target=/usr/local/cargo/registry \
45+
--mount=type=cache,target=/usr/local/cargo/git \
46+
cargo chef cook --release --recipe-path recipe.json
2547

2648
COPY . .
2749

50+
# -- Step 2: build binary into normal target directory
51+
ENV CARGO_TARGET_DIR=/app/target
2852
RUN cargo build -p orion-server --release
2953

3054
# ────── Stage 3: Runtime ──────
3155
FROM debian:bookworm-slim
3256

3357
RUN apt-get update && \
34-
apt-get install -y --no-install-recommends ca-certificates curl && \
35-
rm -rf /var/lib/apt/lists/*
58+
apt-get install -y --no-install-recommends \
59+
ca-certificates \
60+
curl \
61+
libssl3 \
62+
&& rm -rf /var/lib/apt/lists/*
3663

3764
# Copy built executable
3865
COPY --from=builder /app/target/release/orion-server /usr/local/bin/orion-server

orion/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ name = "orion"
33
version = "0.1.0"
44
edition = "2024"
55

6+
[[bin]]
7+
name = "orion"
8+
path = "src/main.rs"
9+
10+
611
[dependencies]
712
tokio = { workspace = true, features = ["rt-multi-thread", "fs", "process"] }
813
tracing = { workspace = true }

orion/audit/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "audit"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[lib]
7+
name = "audit"
8+
path = "src/lib.rs"
9+
610
[dependencies]
711
anyhow = "1.0"
812
clap = {version = "4.1.4", features = ["derive"]}

orion/buck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[lib]
7+
name = "td_util_buck"
78
path = "lib.rs"
89

910
[dependencies]

0 commit comments

Comments
 (0)