@@ -107,9 +107,8 @@ RUN --mount=type=bind,source=src,target=src \
107107 --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
108108 --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
109109 --mount=type=cache,target=/app/target/ \
110- --mount=type=cache,target=/usr/local/cargo/git/db \
111- --mount=type=cache,target=/usr/local/cargo/registry/ \
112- cargo build --locked --release && \
110+ --mount=type=cache,target=/var/cache/cargo \
111+ CARGO_HOME=/var/cache/cargo cargo build --locked --release && \
113112 cp ./target/release/$APP_NAME /bin/server
114113
115114# ###############################################################################
@@ -164,9 +163,8 @@ RUN --mount=type=bind,source=src,target=src \
164163 --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
165164 --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
166165 --mount=type=cache,target=/app/target/ \
167- --mount=type=cache,target=/usr/local/cargo/git/db \
168- --mount=type=cache,target=/usr/local/cargo/registry/ \
169- cargo build --locked --release && \
166+ --mount=type=cache,target=/var/cache/cargo \
167+ CARGO_HOME=/var/cache/cargo cargo build --locked --release && \
170168 cp ./target/release/$APP_NAME /bin/server
171169
172170# ###############################################################################
@@ -206,6 +204,11 @@ CMD ["/bin/server"]
206204{{< /tab >}}
207205{{< /tabs >}}
208206
207+ The cache mount uses a single ` CARGO_HOME ` so concurrent builds coordinate
208+ access through Cargo's cache lock. Setting ` CARGO_HOME ` also changes where
209+ Cargo looks for global configuration and credentials. Project-level
210+ ` .cargo/config.toml ` files are unaffected.
211+
209212For building an image, only the Dockerfile is necessary. Open the Dockerfile
210213in your favorite IDE or text editor and see what it contains. To learn more
211214about Dockerfiles, see the [ Dockerfile reference] ( /reference/dockerfile.md ) .
@@ -591,21 +594,21 @@ For the sample application, you'll use a variation of the backend from the react
591594 WORKDIR /app
592595
593596 # Build the application.
594- # Leverage a cache mount to /usr/local /cargo/registry/
595- # for downloaded dependencies and a cache mount to /app/target/ for
596- # compiled dependencies which will speed up subsequent builds.
597+ # Leverage a cache mount to /var/cache /cargo for downloaded dependencies
598+ # and a cache mount to /app/target/ for compiled dependencies which will
599+ # speed up subsequent builds.
597600 # Leverage a bind mount to the src directory to avoid having to copy the
598601 # source code into the container. Once built, copy the executable to an
599602 # output directory before the cache mounted /app/target is unmounted.
600603 RUN --mount=type=bind,source=src,target=src \
601604 --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
602605 --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
603606 --mount=type=cache,target=/app/target/ \
604- --mount=type=cache,target=/usr/local /cargo/registry/ \
607+ --mount=type=cache,target=/var/cache /cargo \
605608 --mount=type=bind,source=migrations,target=migrations \
606609 <<EOF
607610 set -e
608- cargo build --locked --release
611+ CARGO_HOME=/var/cache/cargo cargo build --locked --release
609612 cp ./target/release/$APP_NAME /bin/server
610613 EOF
611614
0 commit comments