Skip to content

Don't store pip and uv cache files in our docker images#3344

Merged
copybara-service[bot] merged 1 commit intomainfrom
igorts/docker_prune
Mar 6, 2026
Merged

Don't store pip and uv cache files in our docker images#3344
copybara-service[bot] merged 1 commit intomainfrom
igorts/docker_prune

Conversation

@igorts-git
Copy link
Copy Markdown
Collaborator

@igorts-git igorts-git commented Mar 6, 2026

Description

uv and pip create a copy of everything they fetch under the /root/.cache dir. Those cached files are not used at runtime.
This cache adds about 4 GB to the docker image size.

We use the Dockerfile syntax below to avoid creating the /root/.cache/[pip|uv] dirs inside the final image:

RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=cache,target=/root/.cache/uv \
    pip install package

Tests

Manually built a couple of variants of the docker images. Confirmed that they get smaller and still run under XPK.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@igorts-git igorts-git marked this pull request as ready for review March 6, 2026 21:58
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 6, 2026

🤖 Hi @igorts-git, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Review Summary

This PR correctly implements BuildKit cache mounts (--mount=type=cache) to prevent pip and uv from polluting the final Docker image with cache files. The change successfully addresses the 4 GB image size reduction goal.

🔍 General Feedback

  • Cache utilization vs. image bloat: Using Docker cache mounts is the ideal solution for keeping package installation caches out of the final image. However, some pip install commands still include the --no-cache-dir flag. Removing this flag allows pip to actually populate and use the mounted cache directory during the build, which will speed up subsequent local rebuilds while continuing to keep the image small.
  • Uninstall commands: Several pip uninstall commands now use the cache mount. Since uninstalling packages does not read or write to the cache, this mount is redundant and can be omitted.

Comment thread dependencies/dockerfiles/maxtext_post_training_local_dependencies.Dockerfile Outdated
Comment thread dependencies/dockerfiles/maxtext_post_training_local_dependencies.Dockerfile Outdated
Comment thread dependencies/dockerfiles/maxtext_post_training_local_dependencies.Dockerfile Outdated
Comment thread dependencies/dockerfiles/maxtext_post_training_local_dependencies.Dockerfile Outdated
Comment thread dependencies/dockerfiles/maxtext_post_training_local_dependencies.Dockerfile Outdated
Reduces docker image size by removing pip and uv cache files.
@igorts-git igorts-git force-pushed the igorts/docker_prune branch from 88eb51e to e56309a Compare March 6, 2026 22:33
Copy link
Copy Markdown
Collaborator

@richjames0 richjames0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@copybara-service copybara-service Bot merged commit a0fceb5 into main Mar 6, 2026
40 checks passed
@copybara-service copybara-service Bot deleted the igorts/docker_prune branch March 6, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants