# Build the application from source.
FROM rust:1.88.0-slim@sha256:d62f2139b1f523b4b048c59af6c5e8f2cbf6ab04e91ff87b2b9afb3fab3b930a AS rust-builder
ENV CARGO_HOME="/cache/cargo"
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
COPY src/ ./src/
# Install musl-tools to cross-compile the application for a lean image.
RUN --mount=type=cache,target="/var/cache/" \
--mount=type=cache,target="/var/lib/apt/lists/" \
apt-get update && apt-get install -y --no-install-recommends musl-tools
# Build the application for the musl target.
RUN --mount=type=cache,target=${CARGO_HOME} \
rustup target add x86_64-unknown-linux-musl && \
cargo build --release --locked --target x86_64-unknown-linux-musl
# Deploy the application binary into a lean image.
FROM gcr.io/distroless/static-debian12:debug@sha256:112141358ce173d72800bb0e85b4d9dda7505ffc37203e70e475425d5a0d097b AS runtime
LABEL maintainer="DeadNews <deadnewsgit@gmail.com>"
ENV SERVICE_PORT=8000
COPY --from=rust-builder /app/target/x86_64-unknown-linux-musl/release/deadnews-template-rust /usr/deadnews-template-rust
RUN ["/busybox/sh", "-c", "ln -s /busybox/sh /bin/sh"]
USER nonroot:nonroot
EXPOSE ${SERVICE_PORT}
HEALTHCHECK NONE
ENTRYPOINT ["/bin/deadnews-template-rust"]