Skip to content

fix(docker): Correct cargo dependency caching in build stages #9410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from

Conversation

gustavovalverde
Copy link
Member

Motivation

Caching was not being used correctly in our Dockerfile

Depends-On: #9344

Solution

Previously, the Dockerfile used cache mounts targeting specific subdirectories (/usr/local/cargo/registry/ and /usr/local/cargo/git/db) which did not align with the CARGO_HOME environment variable (/home/zebra/.cargo). This prevented Cargo from utilizing the cache for downloaded dependencies and the registry index, causing them to be re-downloaded on subsequent builds.

This change replaces the specific subdirectory mounts with a single cache mount targeting the ${CARGO_HOME} directory directly in both the tests and release stages. This ensures that BuildKit's cache is correctly utilized by Cargo, significantly speeding up rebuilds by avoiding unnecessary downloads.

Tests

This should be faster than our actual rebuilds, and also than #9328

Specifications & References

PR Checklist

  • The PR name is suitable for the release notes.
  • The solution is tested.
  • The documentation is up to date.
  • The PR has a priority label.
  • If the PR shouldn't be in the release notes, it has the
    C-exclude-from-changelog label.

Previously, the Dockerfile used cache mounts targeting specific subdirectories (`/usr/local/cargo/registry/` and `/usr/local/cargo/git/db`) which did not align with the `CARGO_HOME` environment variable (`/home/zebra/.cargo`).

This prevented Cargo from utilizing the cache for downloaded dependencies and the registry index, causing them to be re-downloaded on subsequent builds.

This change replaces the specific subdirectory mounts with a single cache mount targeting the `${CARGO_HOME}` directory directly in both the `tests` and `release` stages. This ensures that BuildKit's cache is correctly utilized by Cargo, significantly speeding up rebuilds by avoiding unnecessary downloads.

This also adds a `--link` parameter to the `COPY` commands, to isolate this layers and avoid invalidating following COPY commands
@gustavovalverde gustavovalverde added A-devops Area: Pipelines, CI/CD and Dockerfiles C-enhancement Category: This is an improvement I-slow Problems with performance or responsiveness P-High 🔥 C-exclude-from-changelog Category: The PR should be excluded from the changelog and release notes labels Apr 10, 2025
@gustavovalverde gustavovalverde requested a review from a team as a code owner April 10, 2025 11:13
@gustavovalverde gustavovalverde requested review from arya2 and removed request for a team April 10, 2025 11:13
@gustavovalverde gustavovalverde marked this pull request as draft April 10, 2025 11:13
Copy link
Contributor

mergify bot commented Apr 10, 2025

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🔴 ⛓️ Depends-On Requirements

This rule is failing.

Requirement based on the presence of Depends-On in the body of the pull request

@gustavovalverde
Copy link
Member Author

gustavovalverde commented Apr 11, 2025

@gustavovalverde gustavovalverde changed the base branch from main to refactor-conf April 11, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-devops Area: Pipelines, CI/CD and Dockerfiles C-enhancement Category: This is an improvement C-exclude-from-changelog Category: The PR should be excluded from the changelog and release notes I-slow Problems with performance or responsiveness P-High 🔥
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant