|
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 |
2 | 5 |
|
3 | 6 | WORKDIR /opt/mega |
4 | 7 |
|
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 |
7 | 10 |
|
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 |
13 | 36 |
|
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 |
18 | 37 |
|
| 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 |
19 | 46 | RUN apt-get update && apt-get install -y \ |
20 | 47 | libssl-dev \ |
21 | 48 | ca-certificates \ |
22 | 49 | clang \ |
23 | 50 | protobuf-compiler \ |
24 | 51 | libprotobuf-dev |
25 | 52 |
|
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 |
27 | 65 | COPY . . |
28 | 66 |
|
29 | | -# build |
| 67 | +# Build arguments to control release / debug mode |
| 68 | +ARG BUILD_TYPE=release |
| 69 | + |
| 70 | +# Build the mono binary |
30 | 71 | RUN if [ "$BUILD_TYPE" = "release" ]; then \ |
31 | 72 | cargo build --release -p mono; \ |
32 | 73 | else \ |
33 | 74 | cargo build -p mono; \ |
34 | 75 | fi |
35 | 76 |
|
36 | | -# final image |
| 77 | + |
| 78 | +# ---------- runtime stage ---------- |
| 79 | +# This is the minimal runtime image containing only the binary and runtime deps |
37 | 80 | FROM debian:bookworm-slim |
38 | 81 |
|
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/* |
40 | 88 |
|
41 | 89 | ARG BUILD_TYPE=release |
42 | 90 |
|
| 91 | +# Copy the compiled binary and startup script |
43 | 92 | COPY --from=builder /opt/mega/target/$BUILD_TYPE/mono /usr/local/bin/mono |
44 | 93 | COPY --from=builder /opt/mega/mono/start-mono.sh /usr/local/bin/start-mono.sh |
45 | 94 |
|
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 |
48 | 97 |
|
| 98 | +# Optional volume for runtime data |
49 | 99 | VOLUME /opt/mega |
50 | 100 |
|
| 101 | +# Default startup command |
51 | 102 | CMD ["bash", "-c", "/usr/local/bin/start-mono.sh"] |
0 commit comments