From 1c38c402b9866466d2ce3e70c3423f98f5c351d3 Mon Sep 17 00:00:00 2001 From: Jonatan Czarniecki Date: Mon, 26 May 2025 20:20:31 +0200 Subject: [PATCH] rework the Dockerfile, add support for aarch64 image --- Cargo.lock | 11 +++++++++++ Cargo.toml | 10 +--------- Dockerfile | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a289e0e..ccfa56b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1531,6 +1531,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" +[[package]] +name = "openssl-src" +version = "300.5.0+3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ce546f549326b0e6052b649198487d91320875da901e7bd11a06d1ee3f9c2f" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.106" @@ -1539,6 +1548,7 @@ checksum = "8bb61ea9811cc39e3c2069f40b8b8e2e70d8569b361f879786cc7ed48b777cdd" dependencies = [ "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] @@ -2025,6 +2035,7 @@ dependencies = [ "faster-hex", "indexmap 2.7.1", "notify", + "openssl", "prometheus", "rand 0.9.0", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 002be6f..2c79952 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ zip = "2.2" notify = "8.0" # Crypto +openssl = { version = "0.10", features = ["vendored"] } ring = "0.17" rand = "0.9" @@ -42,12 +43,3 @@ axum = { version = "0.8", features = ["ws", "macros", "http2"] } tower-http = { version = "0.6", features = ["trace"] } tokio = { version = "1.41", features = ["full"] } prometheus = { version = "0.13.4", features = ["process"] } - -[dev-dependencies] -cross = "0.2.5" - -[workspace.metadata.cross.target.x86_64-unknown-linux-gnu] -pre-build = [ - "dpkg --add-architecture $CROSS_DEB_ARCH", - "apt-get update && apt-get --assume-yes install libssl-dev:$CROSS_DEB_ARCH" -] diff --git a/Dockerfile b/Dockerfile index e40b8aa..93f1689 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,12 @@ +ARG ALPINE_VERSION="3.21" +ARG RUST_VERSION="1.85" ## Chef -# FROM clux/muslrust:stable AS chef -FROM rust:1.85-alpine3.21 AS chef +FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine${ALPINE_VERSION} AS chef USER root -RUN apk add --no-cache musl-dev libressl-dev -RUN cargo install cargo-chef +RUN apk add --no-cache musl-dev libressl-dev zig perl make +RUN cargo install --locked cargo-chef cargo-zigbuild WORKDIR /build +ENV PKG_CONFIG_SYSROOT_DIR=/ ## Planner FROM chef AS planner @@ -13,19 +15,35 @@ COPY src src RUN cargo chef prepare --recipe-path recipe.json ## Builder -FROM chef AS builder +FROM chef AS builder COPY --from=planner /build/recipe.json recipe.json +# Map Docker's TARGETPLATFORM to Rust's target +# and save the result to a .env file +ARG TARGETPLATFORM +RUN <&2; exit 1;; +esac +echo export CARGO_BUILD_TARGET="${CARGO_BUILD_TARGET}" > /tmp/builder.env +rustup target add "${CARGO_BUILD_TARGET}" +EOT # Build dependencies - this is the caching Docker layer! -RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json +RUN . /tmp/builder.env && \ + cargo chef cook --recipe-path recipe.json --release --zigbuild # Build application COPY Cargo.toml Cargo.lock ./ COPY src src -RUN cargo build --release --target x86_64-unknown-linux-musl --bin sculptor +RUN . /tmp/builder.env && \ + cargo zigbuild -r --bin sculptor && \ + # Link the right output directory to a well known location for easier access when copying to the runtime image + ln -s "$PWD/target/$CARGO_BUILD_TARGET/release" /tmp/build-output ## Runtime -FROM alpine:3.21 AS runtime +FROM alpine:${ALPINE_VERSION} AS runtime WORKDIR /app -COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/sculptor /app/sculptor +COPY --from=builder /tmp/build-output/sculptor /app/sculptor RUN apk add --no-cache tzdata ENV TZ=Etc/UTC @@ -34,4 +52,4 @@ VOLUME [ "/app/data" ] VOLUME [ "/app/logs" ] EXPOSE 6665/tcp -ENTRYPOINT [ "./sculptor" ] \ No newline at end of file +ENTRYPOINT [ "./sculptor" ]