@@ -15,23 +15,20 @@ WORKDIR /usr/src/app
1515# Leverage a cache mount to /usr/local/cargo/registry/
1616# for downloaded dependencies and a cache mount to /app/target/ for
1717# compiled dependencies which will speed up subsequent builds.
18- # Leverage a bind mount to the src directory to avoid having to copy the
19- # source code into the container. Once built, copy the executable to an
20- # output directory before the cache mounted /app/target is unmounted.
21- RUN --mount=type=bind,source=src,target=src \
22- --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
23- --mount=type=cache,target=/app/target/ \
24- --mount=type=cache,target=/usr/local/cargo/registry/ \
25- <<EOF
26- set -e
27- cargo build --release
28- EOF
18+ # Once built, copy the executable to an output directory before
19+ # the cache mounted /app/target is unmounted.
20+ COPY Cargo.toml ./
21+ COPY src ./src
22+ RUN --mount=type=cache,target=/usr/src/app/target \
23+ --mount=type=cache,target=/usr/local/cargo/registry \
24+ cargo build --release --bin ${APP_NAME} && cp ./target/release/${APP_NAME} ./${APP_NAME}
2925
3026# ###############################################################################
3127# Create a new stage for running the application that contains the minimal
3228# runtime dependencies for the application. This often uses a different base
3329# image from the build stage where the necessary files are copied from the build
3430# stage.
31+
3532FROM debian:bookworm-slim AS final
3633ARG APP_NAME
3734
@@ -51,7 +48,7 @@ RUN adduser \
5148USER appuser
5249
5350# Copy the executable from the "build" stage.
54- COPY --from=builder /usr/src/app/target/release/ ${APP_NAME} .
51+ COPY --from=builder /usr/src/app/${APP_NAME} .
5552
5653# Copy the necessary files.
5754COPY templates ./templates
0 commit comments