Use printf instead of heredoc for cargo config — heredoc inside a conditional RUN block confuses Docker's parser (fi becomes an unknown instruction). The config is always written; unused linker entries are harmless on native builds.
78 lines
3 KiB
Text
78 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 (ignored on native builds).
|
|
RUN mkdir -p /root/.cargo && printf '\
|
|
[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 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"]
|