#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# builder image
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# start from an existing image
FROM rust:alpine AS builder

# create a working directory and execute every remaining command from it
WORKDIR /app

# copy the specified file(s) from the host to the working directory
COPY Cargo.toml .

# run this command in the guest system (dependency compilation in cache)
RUN --mount=type=cache,target=/app/target \
    mkdir src && echo 'fn main() {}' >src/main.rs \
 && cargo build --release && rm -rf src target/release/rs_backend

# copy the specified file(s) from the host to the working directory
COPY src/ src/

# run this command in the guest system (program compilation)
RUN --mount=type=cache,target=/app/target \
    find src -exec touch {} + \
 && cargo build --release && cp target/release/rs_backend .

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# final image
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# start from an EMPTY image
FROM scratch

# copy the compiled program from the builder image
COPY --from=builder /app/rs_backend /

# run this command by default
ENTRYPOINT ["/rs_backend"]
