|
1 | | -# Étape 1 : Compilation avec nightly + build-std |
| 1 | +# Étape 1 : Compilation |
2 | 2 | FROM rust:1.83-alpine AS builder |
3 | 3 |
|
| 4 | +ARG TARGETARCH |
| 5 | + |
4 | 6 | RUN apk add --no-cache musl-dev upx |
5 | 7 |
|
6 | | -# Installer nightly et rust-src |
7 | | -RUN rustup toolchain install nightly \ |
8 | | - && rustup component add rust-src --toolchain nightly |
| 8 | +# Déterminer la target en fonction de l'architecture |
| 9 | +# Si TARGETARCH n'est pas défini (legacy builder), on détecte l'arch native |
| 10 | +RUN if [ -z "$TARGETARCH" ]; then \ |
| 11 | + case "$(uname -m)" in \ |
| 12 | + x86_64) TARGETARCH=amd64 ;; \ |
| 13 | + aarch64) TARGETARCH=arm64 ;; \ |
| 14 | + esac; \ |
| 15 | + fi && \ |
| 16 | + case "$TARGETARCH" in \ |
| 17 | + amd64) echo "x86_64-unknown-linux-musl" > /tmp/rust_target ;; \ |
| 18 | + arm64) echo "aarch64-unknown-linux-musl" > /tmp/rust_target ;; \ |
| 19 | + *) echo "Unsupported arch: $TARGETARCH" && exit 1 ;; \ |
| 20 | + esac && \ |
| 21 | + rustup target add $(cat /tmp/rust_target) |
9 | 22 |
|
10 | 23 | WORKDIR /app |
11 | 24 |
|
12 | 25 | # Cache des dépendances |
13 | 26 | COPY Cargo.toml Cargo.lock* ./ |
14 | | -RUN mkdir src && echo "fn main() {}" > src/main.rs \ |
15 | | - && RUSTFLAGS="-Zunstable-options -Cpanic=abort" cargo +nightly build --release \ |
16 | | - -Z build-std=std,panic_abort \ |
17 | | - --target x86_64-unknown-linux-musl \ |
| 27 | +RUN RUST_TARGET=$(cat /tmp/rust_target) \ |
| 28 | + && mkdir src && echo "fn main() {}" > src/main.rs \ |
| 29 | + && cargo build --release --target $RUST_TARGET \ |
18 | 30 | && rm -rf src |
19 | 31 |
|
20 | | -# Compilation du binaire avec build-std |
| 32 | +# Compilation du binaire |
21 | 33 | COPY src ./src |
22 | | -RUN touch src/main.rs \ |
23 | | - && RUSTFLAGS="-Zunstable-options -Cpanic=abort" cargo +nightly build --release \ |
24 | | - -Z build-std=std,panic_abort \ |
25 | | - --target x86_64-unknown-linux-musl \ |
26 | | - && strip target/x86_64-unknown-linux-musl/release/naolametric \ |
27 | | - && upx --best --lzma target/x86_64-unknown-linux-musl/release/naolametric |
| 34 | +RUN RUST_TARGET=$(cat /tmp/rust_target) \ |
| 35 | + && touch src/main.rs \ |
| 36 | + && cargo build --release --target $RUST_TARGET \ |
| 37 | + && strip target/$RUST_TARGET/release/naolametric \ |
| 38 | + && upx --best --lzma target/$RUST_TARGET/release/naolametric \ |
| 39 | + && cp target/$RUST_TARGET/release/naolametric /naolametric-bin |
28 | 40 |
|
29 | 41 | # Étape 2 : Image scratch |
30 | 42 | FROM scratch |
31 | 43 |
|
32 | | -COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/naolametric /naolametric |
| 44 | +COPY --from=builder /naolametric-bin /naolametric |
33 | 45 |
|
34 | 46 | ENV PORT=8080 |
35 | 47 | EXPOSE 8080 |
|
0 commit comments