Skip to content

Commit 2e4e59f

Browse files
authored
Merge pull request #25616 from dvdksn/codex/fix-25597-cargo-cache
docs: fix concurrent Rust cache mounts
1 parent dc39aa2 commit 2e4e59f

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

content/guides/rust.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
209212
For building an image, only the Dockerfile is necessary. Open the Dockerfile
210213
in your favorite IDE or text editor and see what it contains. To learn more
211214
about 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

content/manuals/build/cache/optimize.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,15 @@ RUN --mount=type=cache,target=/root/.gem \
262262

263263
```dockerfile
264264
RUN --mount=type=cache,target=/app/target/ \
265-
--mount=type=cache,target=/usr/local/cargo/git/db \
266-
--mount=type=cache,target=/usr/local/cargo/registry/ \
267-
cargo build
265+
--mount=type=cache,target=/var/cache/cargo \
266+
CARGO_HOME=/var/cache/cargo cargo build
268267
```
269268

269+
The cache mount uses a single `CARGO_HOME` so concurrent builds coordinate
270+
access through Cargo's cache lock. Setting `CARGO_HOME` also changes where
271+
Cargo looks for global configuration and credentials. Project-level
272+
`.cargo/config.toml` files are unaffected.
273+
270274
{{< /tab >}}
271275
{{< tab name=".NET" >}}
272276

0 commit comments

Comments
 (0)