Multi-platform Docker build: amd64, arm64, armv7, armv6

Dockerfile now uses BuildKit TARGETARCH/TARGETVARIANT to pick the Rust
cross-compilation target automatically. The build stage always runs on
the host platform for speed.

Makefile provides named targets:
  make up-amd64   # Mac Intel / Linux desktop
  make up-arm64   # Mac M1/M2/M3, Pi 4/5 (64-bit OS)
  make up-armv7   # Pi 2/3/4 (32-bit OS)
  make up-armv6   # Pi Zero / Pi 1
This commit is contained in:
Claude 2026-03-20 09:55:53 +00:00
parent 3c0adff880
commit 588e74a626
No known key found for this signature in database
3 changed files with 76 additions and 7 deletions

View file

@ -1,12 +1,40 @@
# syntax=docker/dockerfile:1
# ── Build stage ─────────────────────────────────────────────────────────────── # ── Build stage ───────────────────────────────────────────────────────────────
FROM rust:1.94-slim-bookworm AS builder # Always run the compiler on the *build* host for speed; cross-compile to target.
FROM --platform=$BUILDPLATFORM rust:1.86-slim-bookworm AS builder
ARG TARGETARCH
ARG TARGETVARIANT
# Cross-compilation toolchains for every supported target.
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
pkg-config \ pkg-config \
musl-tools \ gcc-aarch64-linux-gnu \
gcc-arm-linux-gnueabihf \
gcc-arm-linux-gnueabi \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
RUN rustup target add armv7-unknown-linux-gnueabihf # 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 for each foreign target.
RUN 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
WORKDIR /build WORKDIR /build
@ -14,12 +42,18 @@ WORKDIR /build
COPY Cargo.toml Cargo.lock* ./ COPY Cargo.toml Cargo.lock* ./
COPY server/Cargo.toml ./server/ COPY server/Cargo.toml ./server/
RUN mkdir -p server/src && echo 'fn main(){}' > server/src/main.rs RUN mkdir -p server/src && echo 'fn main(){}' > server/src/main.rs
RUN cargo build --release --target armv7-unknown-linux-gnueabihf -p hiy-server 2>/dev/null || true RUN TARGET=$(cat /rust_target) && \
cargo build --release --target "$TARGET" -p hiy-server 2>/dev/null || true
RUN rm -f server/src/main.rs RUN rm -f server/src/main.rs
# Build actual source. # Build actual source.
COPY server/src ./server/src COPY server/src ./server/src
RUN touch server/src/main.rs && cargo build --release --target armv7-unknown-linux-gnueabihf -p hiy-server 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 ───────────────────────────────────────────────────────────── # ── Runtime stage ─────────────────────────────────────────────────────────────
FROM debian:bookworm-slim FROM debian:bookworm-slim
@ -30,11 +64,10 @@ RUN apt-get update && apt-get install -y \
curl \ curl \
bash \ bash \
python3 \ python3 \
# Docker CLI (no daemon — uses host socket)
docker.io \ docker.io \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
COPY --from=builder /build/target/armv7-unknown-linux-gnueabihf/release/hiy-server /usr/local/bin/hiy-server COPY --from=builder /usr/local/bin/hiy-server /usr/local/bin/hiy-server
WORKDIR /app WORKDIR /app

35
infra/Makefile Normal file
View file

@ -0,0 +1,35 @@
# HIY — docker compose helpers
# Usage: make <target>
#
# Supported platforms
# up-amd64 — x86-64 (Mac Intel, Linux desktop, CI)
# up-arm64 — ARM 64-bit (Mac M1/M2/M3, Pi 4 / Pi 5 running 64-bit OS)
# up-armv7 — ARM 32-bit v7 (Pi 2 / Pi 3 / Pi 4 running 32-bit OS)
# up-armv6 — ARM 32-bit v6 (Pi Zero, Pi 1)
#
# The default target builds for linux/amd64 (same as 'up-amd64').
COMPOSE = docker compose
UP = $(COMPOSE) up --build
.PHONY: up up-amd64 up-arm64 up-armv7 up-armv6 down logs
up: up-amd64
up-amd64:
PLATFORM=linux/amd64 $(UP)
up-arm64:
PLATFORM=linux/arm64 $(UP)
up-armv7:
PLATFORM=linux/arm/v7 $(UP)
up-armv6:
PLATFORM=linux/arm/v6 $(UP)
down:
$(COMPOSE) down
logs:
$(COMPOSE) logs -f server

View file

@ -18,6 +18,7 @@ services:
# ── Control plane ───────────────────────────────────────────────────────── # ── Control plane ─────────────────────────────────────────────────────────
server: server:
platform: ${PLATFORM:-linux/amd64}
build: build:
context: .. context: ..
dockerfile: infra/Dockerfile.server dockerfile: infra/Dockerfile.server