fix: drop cross-compilation, build natively in Dockerfile

podman-compose does not populate BUILDPLATFORM/TARGETARCH build args, so
the platform-detection logic always fell back to x86_64 — even on arm64.
This caused cc-rs to look for 'x86_64-linux-gnu-gcc' instead of 'gcc'.

Replace the entire cross-compile scaffolding with a plain native build:
  cargo build --release (no --target)

Cargo targets the host platform automatically. If cross-compilation is
ever needed it can be reintroduced with a properly-tested setup.

https://claude.ai/code/session_01FKCW3FDjNFj6jve4niMFXH
This commit is contained in:
Claude 2026-03-24 16:25:48 +00:00
parent a873049e96
commit 0bd7b44b81
No known key found for this signature in database

View file

@ -1,70 +1,28 @@
# 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
# Native build: Cargo targets the host platform automatically.
# No --target flag means no cross-compiler confusion regardless of which
# arch podman-compose runs on (x86_64, arm64, armv7…).
FROM 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.
# gcc is required by cc-rs (used by aes-gcm / ring build scripts).
# rust:slim does not include a C compiler.
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.
# Cache dependency compilation separately from application 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 cargo build --release -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
RUN touch server/src/main.rs && \
cargo build --release -p hiy-server
# ── Runtime stage ─────────────────────────────────────────────────────────────
FROM docker.io/library/debian:bookworm-slim
@ -78,7 +36,7 @@ RUN apt-get update && apt-get install -y \
podman \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/bin/hiy-server /usr/local/bin/hiy-server
COPY --from=builder /build/target/release/hiy-server /usr/local/bin/hiy-server
WORKDIR /app