FROM rust:slim-trixie
# lsof used to check if server is up
RUN apt-get update && apt-get install -y lsof
COPY ./Cargo.toml .
COPY ./Cargo.lock .
# Remove non-dependencies to prevent errors from looking for files that dont exist yet.
RUN sed --in-place '36,65d' ./Cargo.toml
RUN cat ./Cargo.toml
# Cache crates in separate layer in case of build failures.
# Fetch crates before src/ COPY so crates aren't fetched every time src/ changes.
RUN mkdir ./src && \
# Create a fake source file so cargo won't error during build.
echo 'fn main() { println!("This is not the binary you are looking for!"); }' > ./src/main.rs && \
# Fetch and compile dependencies
cargo build --release && \
# Clean up the fake files
rm -Rvf ./src
# Copy the actual code and chown main.rs for the next step.
COPY --chown=1000 . .
#
# Invalidate rust cache and rebuild
RUN touch ./src/lib.rs && cargo build --release