Skip to content

Cache mounts for Rust packages break concurrent builds #25597

Description

@fence-sitter

Is this a docs issue?

  • My issue is about the documentation content or website

Type of issue

Information is incorrect

Description

Rust language-specific guide and Optimize cache usage in builds say to cache mount /usr/local/cargo/git/db and /usr/local/cargo/registry when building Rust projects. Eg.

RUN --mount=type=cache,target=/usr/local/cargo/git/db \
    --mount=type=cache,target=/usr/local/cargo/registry \
    cargo build

However, I believe this breaks multi-platform builds (Eg. docker buildx build --platform linux/amd64,linux/arm64 .) because while the package cache is shared between builds, the package cache lock is not. The lock can currently be found at $CARGO_HOME/.package-cache, which is /usr/local/cargo/.package-cache inside the container.

Location

https://docs.docker.com/guides/rust/

Suggestion

Potential solutions, worst-to-best in my opinion:

  • Make the cache exclusive, eg. --mount=type=cache,sharing=locked,target=/usr/local/cargo/registry.
    • Why use Docker to do something that Cargo can do by itself?
  • Share the package cache lock file, eg. --mount=type=cache,target=/usr/local/cargo/.package-cache.
  • Cache the entire Cargo home directory, eg. --mount=type=cache,target=/usr/local/cargo,from=rust:1.92,source=/usr/local/cargo.
    • Doing this means we don't care about the internals of Cargo home, but we do have to specify from=rust:1.92 (assuming you are in a FROM rust:1.92 section) so that everything already in Cargo home isn't clobbered.

See https://hackmd.io/@kobzol/S17NS71bh for a good summary of Docker caching with cargo builds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/guidesstatus/confirmedIssue triaged and confirmed as a real problem, ready to fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions