################
##### Builder
FROM rust:1.82.0-slim AS builder

WORKDIR /usr/src

COPY . /usr/src

## Install target platform (Cross-Compilation) --> Needed for Alpine
RUN rustup target add x86_64-unknown-linux-musl

# This is a dummy build to get the dependencies cached.
RUN cargo build --target x86_64-unknown-linux-musl --release

## Touch main.rs to prevent cached release build
RUN touch /usr/src/server/src/main.rs

# This is the actual application build.
RUN cargo build --package server --target x86_64-unknown-linux-musl --release

################
##### Runtime
FROM alpine:3.20.3 AS runtime

# Copy application binary from builder image
COPY --from=builder /usr/src/target/x86_64-unknown-linux-musl/release/saslauthd /usr/local/bin


# Run the application
CMD ["/usr/local/bin/saslauthd"]