FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reorganize dependency installation for better squashing by develra · Pull Request #1523 · Kaggle/docker-python · GitHub

Reorganize dependency installation for better squashing - #1523

Closed
develra wants to merge 1 commit into
mainfrom
optimize-layers-for-better-squashing
Closed

develra wants to merge 1 commit into
mainfrom
optimize-layers-for-better-squashing

Conversation

develra commented Jan 24, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

I'll leave it up to y'all to decide if the changes/risks here are worth the reduction in image size. Thanks!

Reduced image size

Metric Original New Reduction
Image Size 60.1 GB 48.2 GB 11.9 GB (20%)
Filesystem Size 49 GB 44 GB 5 GB (10%)

Note: Image size includes all layers; filesystem size is the actual disk usage inside the container.

  • Added --no-cache to uv pip install (Safe)
    Cache is only useful for repeated installs in the same environment. In Docker builds, each layer is fresh, so cache provides no benefit.

  • Removed Intel MKL numpy (Less sure)

Removed the Intel MKL numpy install from Intel's Anaconda channel. Intel's channel only has numpy 1.26.4 (numpy 1.x), but the base image has numpy 2.0.2. Installing Intel's numpy would downgrade and break packages compiled against numpy 2.x ABI.

The base image's numpy 2.0.2 uses OpenBLAS optimizations and is compatible with all installed packages.

  • Removed preprocessing package (Less sure)
    Package is unmaintained (last release 2017) and requires nltk==3.2.4 which is incompatible with Python 3.11 (inspect.formatargspec was removed). Package hasn't been updated in 7+ years and cannot function on Python 3.11.

  • Updated scikit-learn to 1.5.2 (Less sure)
    Changed from scikit-learn==1.2.2 to scikit-learn==1.5.2. scikit-learn 1.2.2 binary wheels are incompatible with numpy 2.x ABI, causing "numpy.dtype size changed" errors. scikit-learn 1.5.x maintains API compatibility with 1.2.x. The original pin was for eli5/learntools compatibility, which should work with 1.5.x.

  • Added uv cache cleanup to clean-layer.sh (safe)
    Added /root/.cache/uv/* to the cleanup script. The script only cleaned pip cache, not uv cache. Cache cleanup scripts are run after package installs; cache is not needed at runtime.

develra requested review from calderjo and djherbis January 24, 2026 01:37
I'll leave it up to y'all to decide if the changes/risks here are worth
the reduction in image size. Thanks!

Reduced image size
  ┌─────────────────┬──────────┬─────────┬───────────────┐
  │     Metric      │ Original │ New     │   Reduction   │
  ├─────────────────┼──────────┼─────────┼───────────────┤
  │ Image Size      │ 60.1 GB  │ 48.2 GB │ 11.9 GB (20%) │
  ├─────────────────┼──────────┼─────────┼───────────────┤
  │ Filesystem Size │ 49 GB    │ 44 GB   │ 5 GB (10%)    │
  └─────────────────┴──────────┴─────────┴───────────────┘
  Note: Image size includes all layers; filesystem size is the actual disk usage inside the container.

  - Added --no-cache to uv pip install (Safe)

  Cache is only useful for repeated installs in the same environment. In Docker builds, each layer is fresh, so cache provides no benefit.

  - Removed Intel MKL numpy (Less sure)

  Removed the Intel MKL numpy install from Intel's Anaconda channel.
  Intel's channel only has numpy 1.26.4 (numpy 1.x), but the base image has numpy 2.0.2.
  Installing Intel's numpy would downgrade and break packages compiled against numpy 2.x ABI.

  The base image's numpy 2.0.2 uses OpenBLAS optimizations and is compatible with all installed packages.

  - Removed preprocessing package (Less sure)

  Package is unmaintained (last release 2017) and requires nltk==3.2.4 which is incompatible with Python 3.11 (inspect.formatargspec was removed).
  Package hasn't been updated in 7+ years and cannot function on Python 3.11.

  - Updated scikit-learn to 1.5.2 (Less sure)

  Changed from scikit-learn==1.2.2 to scikit-learn==1.5.2.
  scikit-learn 1.2.2 binary wheels are incompatible with numpy 2.x ABI, causing "numpy.dtype size changed" errors.
  scikit-learn 1.5.x maintains API compatibility with 1.2.x. The original pin was for eli5/learntools compatibility, which should work with 1.5.x.

  - Added uv cache cleanup to clean-layer.sh (safe)

  Added /root/.cache/uv/* to the cleanup script.
  The script only cleaned pip cache, not uv cache.
  Cache cleanup scripts are run after package installs; cache is not needed at runtime.
develra force-pushed the optimize-layers-for-better-squashing branch from 9773e95 to 8abc702 Compare January 24, 2026 01:48
djherbis requested a review from rosbo January 26, 2026 19:52

Copy link
Copy Markdown
Contributor

Adding Vincent since they are on docker ops

rosbo changed the title Reorganzie dependency installation for better squashing Reorganize dependency installation for better squashing Jan 26, 2026

rosbo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Given the unsafe nature of the layer squashing operations, let's focus on the safe changes (deleting files from the uv cache) and making sure clean-layer is called at each layer writing to disk.

Comment thread Dockerfile.tmpl
&& uv pip install --no-cache --system --force-reinstall --no-deps torchtune gensim "scipy<=1.15.3" "huggingface-hub==0.36.0" "google-cloud-translate==3.12.1" "numpy==2.0.2" "pandas==2.2.2" \
&& uv pip install --no-cache --system --force-reinstall "protobuf==5.29.5" \
&& uv pip install --no-cache --system --force-reinstall "nltk>=3.9.1" \
&& rm -rf /root/.cache/uv /root/.cache/pip

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can you move the ADD clean-layer.sh /tmp/clean-layer.sh line above and call clean-layer.sh here instead.

Comment thread Dockerfile.tmpl
# b/468383498: numpy will auto-upgrade to 2.4.x, which causes issues with numerous packages
# b/468367647: Unpin protobuf, version greater than v5.29.5 causes issues with numerous packages
# b/408298750: We reinstall nltk because older versions have: `AttributeError: module 'inspect' has no attribute 'formatargspec'`
RUN uv pip install --no-cache \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If we are cleaning up the layer at the end, is passing --no-cache redundant?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Probably - it takes a little while to run each experiment as I need to build the whole container, but I'll try to drop it and just be more specific about the clean-layer.sh

Comment thread Dockerfile.tmpl
RUN echo "$GIT_COMMIT" > /etc/git_commit && echo "$BUILD_DATE" > /etc/build_date

# Final cleanup
RUN rm -rf /root/.cache/uv /root/.cache/pip /tmp/clean-layer.sh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Removing the .cache directory at this point is pointless given it has been saved in previous layers anyway.

Comment thread Dockerfile.tmpl
# Stage 2: Squash the base + reinstalls to eliminate layer bloat
# =============================================================================
FROM scratch AS clean-base
COPY --from=base-reinstalls / /

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This doesn't copy all the changes to environment variable and whatnot. Causing issues like:

�[0m - InvalidDefaultArgInFrom: Default value for ARG ${BASE_IMAGE}:${BASE_IMAGE_TAG} results in empty or invalid base image name (line 9)
 - UndefinedVar: Usage of undefined variable '$PYTHONPATH' (line 153)
ERROR: failed to build: failed to solve: process "/bin/sh -c uv pip install --no-cache --system \"pycuda\"" did not complete successfully: exit code: 1

rosbo commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

Closed in favor of #1532.

We won't do the layer squashing for now because it is not safe: #1523 (comment)

rosbo closed this Jan 30, 2026
rosbo added a commit that referenced this pull request Jan 30, 2026
)

Docker layers.

We were not clearing the `uv` cache adding extra bloat to each our our
docker layer. Added more background on this here:
https://b.corp.google.com/issues/277208559#comment12

Reasoning why I preferred to use `--no-cache` rather than the
`clean-layer.sh` script:
https://b.corp.google.com/issues/277208559#comment13

Inspired by #1523.

http://b/277208559
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL