1- # Stage 1: Build golang dependencies and binaries
2- FROM ubuntu:25.04 AS build
1+ # Multi-stage Dockerfile for building Juno
32
4- ARG VM_DEBUG
3+ ARG GO_VERSION=1.24.1
4+ ARG RUST_VERSION=1.85.1
5+ ARG JUNO_VERSION=unknown
56
7+ # ==================================================================
8+ # Stage 1: Build Rust libs with cargo-chef
9+ # ==================================================================
10+ FROM lukemathwalker/cargo-chef:0.1.71-rust-${RUST_VERSION}-slim-bookworm AS rust-builder
611
7- RUN apt-get -qq update && \
8- apt-get -qq install curl build-essential git golang upx-ucl libjemalloc-dev libjemalloc2 libbz2-dev
9- RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -q -y
12+ # Install Rust build tools (cbindgen) & C toolchain
13+ RUN apt-get update && \
14+ apt-get install -y --no-install-recommends build-essential && \
15+ cargo install cbindgen --version 0.26.0 && \
16+ rm -rf /var/lib/apt/lists/*
17+
18+ WORKDIR /build
19+
20+ # --- Prepare shared dependencies using cargo-chef ---
21+
22+ # Virtual workspace for cargo-chef
23+ RUN echo '[workspace]\n members = ["starknet_compiler", "core_rust", "vm_rust"]' > Cargo.toml
24+
25+ # Copy manifests for cache
26+ COPY starknet/compiler/rust/Cargo.toml starknet/compiler/rust/Cargo.lock ./starknet_compiler/
27+ COPY core/rust/Cargo.toml core/rust/Cargo.lock ./core_rust/
28+ COPY vm/rust/Cargo.toml vm/rust/Cargo.lock ./vm_rust/
29+
30+ # Prepare cargo-chef recipe (requires dummy lib.rs for libs)
31+ RUN mkdir -p ./starknet_compiler/src ./core_rust/src ./vm_rust/src && \
32+ touch ./starknet_compiler/src/lib.rs ./core_rust/src/lib.rs ./vm_rust/src/lib.rs && \
33+ cargo chef prepare --recipe-path recipe.json
34+
35+ # Cook dependencies (cached layer)
36+ RUN cargo chef cook --release --recipe-path recipe.json
37+
38+ # --- Build the actual projects ---
39+
40+ # Copy source
41+ COPY starknet/compiler/rust/ ./starknet_compiler/
42+ COPY core/rust/ ./core_rust/
43+ COPY vm/rust/ ./vm_rust/
44+
45+ # Args for package/library names
46+ ARG STARKNET_COMPILER_PKG_NAME=juno-starknet-compiler-rs
47+ ARG CORE_RUST_PKG_NAME=juno-starknet-core-rs
48+ ARG VM_RUST_PKG_NAME=juno-starknet-rs
49+ ARG STARKNET_COMPILER_LIB_NAME=juno_starknet_compiler_rs
50+ ARG CORE_RUST_LIB_NAME=juno_starknet_core_rs
51+ ARG VM_RUST_LIB_NAME=juno_starknet_rs
52+
53+ # Build Rust libs & generate C headers
54+ RUN cargo build --release --package ${STARKNET_COMPILER_PKG_NAME} && \
55+ cbindgen --crate ${STARKNET_COMPILER_PKG_NAME} --output target/release/${STARKNET_COMPILER_PKG_NAME}.h
56+
57+ RUN cargo build --release --package ${CORE_RUST_PKG_NAME} && \
58+ cbindgen --crate ${CORE_RUST_PKG_NAME} --output target/release/${CORE_RUST_PKG_NAME}.h
59+
60+ RUN cargo build --release --package ${VM_RUST_PKG_NAME} && \
61+ cbindgen --crate ${VM_RUST_PKG_NAME} --output target/release/${VM_RUST_PKG_NAME}.h
62+
63+ # --- Collect build artifacts ---
64+ RUN mkdir /artifacts && \
65+ cp target/release/lib${STARKNET_COMPILER_LIB_NAME}.a /artifacts/ && \
66+ cp target/release/${STARKNET_COMPILER_PKG_NAME}.h /artifacts/ && \
67+ \
68+ cp target/release/lib${CORE_RUST_LIB_NAME}.a /artifacts/ && \
69+ cp target/release/${CORE_RUST_PKG_NAME}.h /artifacts/ && \
70+ \
71+ cp target/release/lib${VM_RUST_LIB_NAME}.a /artifacts/ && \
72+ cp target/release/${VM_RUST_PKG_NAME}.h /artifacts/
73+
74+
75+ # ==================================================================
76+ # Stage 2: Build Go application
77+ # ==================================================================
78+ FROM golang:${GO_VERSION}-bookworm AS go-builder
79+
80+ ARG JUNO_VERSION
81+
82+ # Install CGO dependencies
83+ RUN apt-get update && \
84+ apt-get install -y --no-install-recommends \
85+ build-essential \
86+ libjemalloc-dev \
87+ libbz2-dev \
88+ && rm -rf /var/lib/apt/lists/*
1089
1190WORKDIR /app
1291
13- # Copy source code
92+ # Cache Go dependencies
93+ COPY go.mod go.sum ./
94+ RUN go mod download && go mod verify
95+
96+ # Copy source
1497COPY . .
1598
16- # Build the project
17- RUN bash -c 'source ~/.cargo/env && VM_DEBUG=${VM_DEBUG} make juno'
99+ # Args needed for artifact paths
100+ ARG STARKNET_COMPILER_PKG_NAME=juno-starknet-compiler-rs
101+ ARG CORE_RUST_PKG_NAME=juno-starknet-core-rs
102+ ARG VM_RUST_PKG_NAME=juno-starknet-rs
103+ ARG STARKNET_COMPILER_LIB_NAME=juno_starknet_compiler_rs
104+ ARG CORE_RUST_LIB_NAME=juno_starknet_core_rs
105+ ARG VM_RUST_LIB_NAME=juno_starknet_rs
106+
107+ # Copy Rust libs (.a)
108+ COPY --from=rust-builder /artifacts/lib${STARKNET_COMPILER_LIB_NAME}.a /app/starknet/compiler/rust/target/release/
109+ COPY --from=rust-builder /artifacts/lib${CORE_RUST_LIB_NAME}.a /app/core/rust/target/release/
110+ COPY --from=rust-builder /artifacts/lib${VM_RUST_LIB_NAME}.a /app/vm/rust/target/release/
111+
112+ # Copy Rust headers (.h)
113+ COPY --from=rust-builder /artifacts/${STARKNET_COMPILER_PKG_NAME}.h /app/starknet/compiler/
114+ COPY --from=rust-builder /artifacts/${CORE_RUST_PKG_NAME}.h /app/core/
115+ COPY --from=rust-builder /artifacts/${VM_RUST_PKG_NAME}.h /app/vm/
116+
117+ # Index static libs (for linker)
118+ RUN ranlib /app/starknet/compiler/rust/target/release/lib${STARKNET_COMPILER_LIB_NAME}.a && \
119+ ranlib /app/core/rust/target/release/lib${CORE_RUST_LIB_NAME}.a && \
120+ ranlib /app/vm/rust/target/release/lib${VM_RUST_LIB_NAME}.a
121+
122+ # Configure CGO
123+ ENV CGO_ENABLED=1
124+ ENV CGO_LDFLAGS="-ljemalloc -lm -lpthread -ldl"
125+
126+ # Build Go binary (passing version, stripping symbols)
127+ RUN go build -ldflags="-X main.Version=${JUNO_VERSION} -s -w" -o /app/juno ./cmd/juno/
18128
19- # Compress the executable with UPX
20- RUN upx-ucl /app/build/juno
21129
22- # Stage 2: Build Docker image
23- FROM ubuntu:25.04 AS runtime
130+ # ==================================================================
131+ # Stage 3: Final runtime image
132+ # ==================================================================
133+ FROM debian:bookworm-slim AS final
24134
25- RUN apt-get update && apt-get install -y ca-certificates curl gawk grep libjemalloc-dev libjemalloc2
135+ # Install minimal runtime dependencies
136+ RUN apt-get update && \
137+ DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
138+ ca-certificates \
139+ libjemalloc2 \
140+ libbz2-1.0 \
141+ tzdata \
142+ && apt-get clean && rm -rf /var/lib/apt/lists/*
26143
27- COPY --from=build /app/build/juno /usr/local/bin/
144+ # Copy final binary
145+ COPY --from=go-builder /app/juno /usr/local/bin/
28146
29147ENTRYPOINT ["juno" ]
0 commit comments