FROM alpine:latest AS builder ENV PATH "$PATH:/root/.cargo/bin" # Switch to the nightly build WORKDIR /usr/src/app # Install build dependencies, install rustup & create dummy project RUN apk update && apk add --no-cache openssl-dev build-base curl && \ { curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; } && \ rustup target add x86_64-unknown-linux-musl # Build the dependencies # This is done separately to reduce average compile time # Afterwards, we remove the dummy src directory # Now, we build our own source code COPY Cargo.toml Cargo.lock ./ COPY src/ ./src/ RUN cargo build --release --target x86_64-unknown-linux-musl # Now, we create the actual image FROM scratch # Install dependencies COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/rust-api /rust-api CMD ["/rust-api"]