Skip to content

Commit

Permalink
cross compile docker images instead of emulating
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbasPL committed Nov 13, 2023
1 parent 665a4f6 commit 99b4e5d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
38 changes: 19 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
FROM rust:1-bullseye AS chef
RUN cargo install cargo-chef
WORKDIR /app
FROM --platform=${BUILDPLATFORM:-linux/amd64} tonistiigi/xx AS xx

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM --platform=${BUILDPLATFORM:-linux/amd64} rust:1-bullseye as builder

FROM chef AS builder
WORKDIR /app

RUN apt-get update && apt-get install -y build-essential cmake clang \
&& rm -rf /var/lib/apt/lists/*
COPY --from=xx / /

COPY --from=planner /app/recipe.json recipe.json
# install native dependencies first so we can cache them
RUN apt-get update && \
apt-get install -y build-essential cmake clang llvm g++

# Build dependencies - this is the caching Docker layer!
RUN cargo chef cook --release --recipe-path recipe.json
ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}

RUN xx-apt-get install -y xx-cxx-essentials g++

# Build application
COPY . .
RUN cargo build --release --bin aero2solver

FROM debian:bullseye-slim
RUN ./build.sh

RUN apt-get update && apt-get install -y libgomp1 \
&& rm -rf /var/lib/apt/lists/*
FROM debian:bullseye-slim AS runtime

WORKDIR /app

RUN apt-get update && \
apt-get install -y libgomp1 && \
rm -rf /var/lib/apt/lists/*

COPY ./model/ ./model/
COPY --from=builder /app/target/release/aero2solver ./aero2solver
COPY --from=builder /app/target/aero2solver ./aero2solver

ENTRYPOINT [ "/app/aero2solver" ]
ENTRYPOINT [ "/app/aero2solver" ]
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# set -x
set -e

# shellcheck disable=SC1091
. xx-cargo --setup-target-triple
RUST_TARGET_TRIPLE="$(xx-cargo --print-target-triple)"

if ! xx-info is-cross; then
# remove these clang declarations to avoid linking errors
unset "CARGO_TARGET_$(echo "$RUST_TARGET_TRIPLE" | tr '[:lower:]' '[:upper:]' | tr - _)_LINKER";
unset "CC_$(echo "$RUST_TARGET_TRIPLE" | tr - _)"
fi

cargo build --release --bin aero2solver --target="$RUST_TARGET_TRIPLE"
xx-verify "./target/$RUST_TARGET_TRIPLE/release/aero2solver"
cp "./target/$RUST_TARGET_TRIPLE/release/aero2solver" "./target/aero2solver"

0 comments on commit 99b4e5d

Please sign in to comment.