|
1 | | -FROM rust:slim-bullseye as builder |
| 1 | +FROM --platform=$BUILDPLATFORM rust:slim-bullseye as builder |
| 2 | + |
| 3 | +# Install cross-compilation tools if needed |
| 4 | +ARG BUILDPLATFORM |
| 5 | +ARG TARGETPLATFORM |
| 6 | +RUN if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" = "linux/amd64" ]; then \ |
| 7 | + dpkg --add-architecture arm64 && \ |
| 8 | + apt-get update && \ |
| 9 | + apt-get install -y --no-install-recommends \ |
| 10 | + gcc-aarch64-linux-gnu libc6-dev-arm64-cross && \ |
| 11 | + rustup target add aarch64-unknown-linux-gnu && \ |
| 12 | + rm -rf /var/lib/apt/lists/*; \ |
| 13 | + fi |
| 14 | + |
| 15 | +# Set the correct target |
| 16 | +ARG RUST_TARGET="x86_64-unknown-linux-gnu" |
| 17 | +RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ |
| 18 | + echo "RUST_TARGET=aarch64-unknown-linux-gnu"; \ |
| 19 | + export RUST_TARGET="aarch64-unknown-linux-gnu"; \ |
| 20 | + fi |
| 21 | + |
| 22 | +# Create a new empty project for caching dependencies |
2 | 23 | WORKDIR /usr/src/cfspeedtest |
3 | 24 | COPY Cargo.toml Cargo.lock ./ |
| 25 | +RUN mkdir -p src && \ |
| 26 | + echo "fn main() {}" > src/main.rs && \ |
| 27 | + echo "pub fn dummy() {}" > src/lib.rs && \ |
| 28 | + cargo fetch |
| 29 | + |
| 30 | +# Build the actual application |
4 | 31 | COPY src ./src |
5 | | -RUN cargo install --path . |
| 32 | +RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ |
| 33 | + RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" \ |
| 34 | + cargo build --release --target aarch64-unknown-linux-gnu && \ |
| 35 | + cp target/aarch64-unknown-linux-gnu/release/cfspeedtest /usr/local/bin/; \ |
| 36 | + else \ |
| 37 | + cargo build --release && \ |
| 38 | + cp target/release/cfspeedtest /usr/local/bin/; \ |
| 39 | + fi |
6 | 40 |
|
7 | | -FROM debian:bullseye-slim |
| 41 | +FROM --platform=$TARGETPLATFORM debian:bullseye-slim |
8 | 42 | RUN apt-get update && apt-get install -y --no-install-recommends tini && rm -rf /var/lib/apt/lists/* |
9 | | -COPY --from=builder /usr/local/cargo/bin/cfspeedtest /usr/local/bin/cfspeedtest |
| 43 | +COPY --from=builder /usr/local/bin/cfspeedtest /usr/local/bin/cfspeedtest |
10 | 44 |
|
11 | 45 | # tini will be PID 1 and handle signal forwarding and process reaping |
12 | 46 | ENTRYPOINT ["/usr/bin/tini", "--", "cfspeedtest"] |
0 commit comments