How to Build Docker Images for Rust Applications

Create a multi-stage Dockerfile to compile Rust with rustup and ship a minimal runtime binary.

Build a Rust Docker image by creating a multi-stage Dockerfile that installs Rust via rustup, compiles the binary in release mode, and copies the result into a minimal runtime image.

# Build stage
FROM rust:1.90 as builder
WORKDIR /app
COPY . .
RUN rustup set profile minimal && cargo build --release

# Runtime stage
FROM debian:bookworm-slim
WORKDIR /app
COPY --from=builder /app/target/release/my_binary /usr/local/bin/
ENTRYPOINT ["my_binary"]

Replace my_binary with your actual binary name found in Cargo.toml.