feat: support proper arm64 docker cross compilation using zig-cc

image-uploads
Jef Roosens 2025-01-12 23:08:03 +01:00
parent 28d0a2cc14
commit faeebf8376
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
1 changed files with 45 additions and 13 deletions

View File

@ -1,31 +1,63 @@
FROM rust:1.83-alpine3.21 AS builder
ARG DI_VER=1.2.5
# This article showed me everything I needed to get this running
# https://dev.to/vladkens/fast-multi-arch-docker-build-for-rust-projects-an1
FROM --platform=$BUILDPLATFORM rust:1.83-alpine3.21 AS chef
WORKDIR /app
RUN apk update && apk add --no-cache build-base
ENV PKGCONFIG_SYSROOTDIR=/
RUN apk update && \
apk add --no-cache build-base musl-dev openssl-dev zig && \
cargo install --locked cargo-zigbuild cargo-chef@0.1.68 && \
rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
# Build dumb-init
RUN wget -O - "https://github.com/Yelp/dumb-init/archive/refs/tags/v${DI_VER}.tar.gz" | tar -xzf - && \
ARG DI_VER=1.2.5
RUN mkdir -p /app/linux/arm64 /app/linux/amd64 && \
wget -O - "https://github.com/Yelp/dumb-init/archive/refs/tags/v${DI_VER}.tar.gz" | tar -xzf - && \
cd "dumb-init-${DI_VER}" && \
make SHELL=/bin/sh && \
mv dumb-init .. && \
make CC='zig cc -target x86_64-linux-musl' SHELL=/bin/sh && \
ls && \
mv dumb-init /app/linux/amd64/dumb-init && \
make CC='zig cc -target aarch64-linux-musl' SHELL=/bin/sh && \
mv dumb-init /app/linux/arm64/dumb-init && \
cd ..
COPY Cargo.toml Cargo.lock ./
COPY --from=planner /app/recipe.json recipe.json
RUN cargo fetch --locked
RUN cargo chef cook \
--recipe-path recipe.json \
--release --zigbuild \
--target x86_64-unknown-linux-musl \
--target aarch64-unknown-linux-musl
COPY . ./
COPY . .
RUN cargo build --release --frozen
RUN cargo zigbuild \
--release \
--target x86_64-unknown-linux-musl \
--target aarch64-unknown-linux-musl && \
cp target/aarch64-unknown-linux-musl/release/calathea /app/linux/arm64/calathea && \
cp target/x86_64-unknown-linux-musl/release/calathea /app/linux/amd64/calathea
FROM alpine:3.21
COPY --from=builder /app/target/release/calathea /app/calathea
COPY --from=builder /app/dumb-init /app/dumb-init
ARG TARGETPLATFORM
COPY --from=builder /app/${TARGETPLATFORM}/calathea /app/calathea
COPY --from=builder /app/${TARGETPLATFORM}/dumb-init /app/dumb-init
COPY templates /app/templates
COPY static /app/static