Hostityourself/infra/Dockerfile.server
Claude 3096d251c6
Fix Dockerfile: skip cross-compilers when building natively
gcc-aarch64-linux-gnu is an x86→arm64 cross-compiler; it doesn't exist
on arm64 hosts (like the Pi). Only install cross-toolchains and set cargo
linker config when BUILDPLATFORM != TARGETPLATFORM.
2026-03-20 10:40:12 +00:00

81 lines
3 KiB
Text

# syntax=docker/dockerfile:1
# ── Build stage ───────────────────────────────────────────────────────────────
# Run the compiler on the *build* host; cross-compile to target when needed.
FROM --platform=$BUILDPLATFORM rust:1.86-slim-bookworm AS builder
ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG TARGETARCH
ARG TARGETVARIANT
# Install cross-compilation toolchains only when actually cross-compiling.
RUN apt-get update && apt-get install -y 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 cross-linker to use — only needed when cross-compiling.
RUN if [ "${BUILDPLATFORM}" != "${TARGETPLATFORM}" ]; then \
mkdir -p /root/.cargo && cat >> /root/.cargo/config.toml <<'EOF'
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
[target.arm-unknown-linux-gnueabi]
linker = "arm-linux-gnueabi-gcc"
EOF
fi
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 debian:bookworm-slim
RUN apt-get update && apt-get install -y \
ca-certificates \
git \
curl \
bash \
python3 \
docker.io \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/bin/hiy-server /usr/local/bin/hiy-server
WORKDIR /app
CMD ["hiy-server"]