# syntax=docker/dockerfile:1 # ── Build stage ─────────────────────────────────────────────────────────────── # Run the compiler on the *build* host; cross-compile to target when needed. FROM --platform=$BUILDPLATFORM docker.io/library/rust:1.94-slim-bookworm AS builder ARG BUILDPLATFORM ARG TARGETPLATFORM ARG TARGETARCH ARG TARGETVARIANT # Install build tools. gcc is always needed: cc-rs (used by ring/aes-gcm deps) # calls the native compiler even on native builds, and rust:slim doesn't # include it. Cross-compilers are added only when actually cross-compiling. RUN apt-get update && apt-get install -y gcc pkg-config && \ if [ "${BUILDPLATFORM}" != "${TARGETPLATFORM}" ]; then \ case "${TARGETARCH}:${TARGETVARIANT}" in \ "arm64:") apt-get install -y gcc-aarch64-linux-gnu ;; \ "arm:v7") apt-get install -y gcc-arm-linux-gnueabihf ;; \ "arm:v6") apt-get install -y gcc-arm-linux-gnueabi ;; \ esac; \ fi && \ rm -rf /var/lib/apt/lists/* # Map TARGETARCH + TARGETVARIANT → Rust target triple, then install it. RUN case "${TARGETARCH}:${TARGETVARIANT}" in \ "amd64:") echo x86_64-unknown-linux-gnu ;; \ "arm64:") echo aarch64-unknown-linux-gnu ;; \ "arm:v7") echo armv7-unknown-linux-gnueabihf ;; \ "arm:v6") echo arm-unknown-linux-gnueabi ;; \ *) echo x86_64-unknown-linux-gnu ;; \ esac > /rust_target && \ rustup target add "$(cat /rust_target)" # Tell Cargo which linker to use for each target. # x86_64 native: use plain gcc (cc-rs would otherwise look for # "x86_64-linux-gnu-gcc" which is the *cross* toolchain, not installed here). RUN mkdir -p /root/.cargo && printf '\ [target.x86_64-unknown-linux-gnu]\n\ linker = "gcc"\n\ \n\ [target.aarch64-unknown-linux-gnu]\n\ linker = "aarch64-linux-gnu-gcc"\n\ \n\ [target.armv7-unknown-linux-gnueabihf]\n\ linker = "arm-linux-gnueabihf-gcc"\n\ \n\ [target.arm-unknown-linux-gnueabi]\n\ linker = "arm-linux-gnueabi-gcc"\n' >> /root/.cargo/config.toml WORKDIR /build # Cache dependencies separately from source. COPY Cargo.toml Cargo.lock* ./ COPY server/Cargo.toml ./server/ RUN mkdir -p server/src && echo 'fn main(){}' > server/src/main.rs RUN TARGET=$(cat /rust_target) && \ cargo build --release --target "$TARGET" -p hiy-server 2>/dev/null || true RUN rm -f server/src/main.rs # Build actual source. COPY server/src ./server/src RUN TARGET=$(cat /rust_target) && \ touch server/src/main.rs && \ cargo build --release --target "$TARGET" -p hiy-server # Normalise binary location so the runtime stage doesn't need to know the target. RUN cp /build/target/"$(cat /rust_target)"/release/hiy-server /usr/local/bin/hiy-server # ── Runtime stage ───────────────────────────────────────────────────────────── FROM docker.io/library/debian:bookworm-slim RUN apt-get update && apt-get install -y \ ca-certificates \ git \ curl \ bash \ python3 \ podman \ && rm -rf /var/lib/apt/lists/* COPY --from=builder /usr/local/bin/hiy-server /usr/local/bin/hiy-server WORKDIR /app CMD ["hiy-server"]