Is it possible/supported/problematic to use cache build mounts (--mount=type=cache) to store installed packages? #2877
Unanswered
tboddyspargo
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Did you ever get a good answer for this? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
My team has been experiencing some pain with building and re-building images locally during development when layers are invalidated. We have a very large python+R service with many dependencies which can take up to 40mins to build. I've leveraging build cache mounts as mitigation tools to get that time down to 10-20mins depending on which layers are invalidated, but we have generally been sticking with the examples in documentation and from the wider community.
At this point, the slowest part of the build process for us is the installation of python packages (not downloading or building wheels). In contrast, when we install these same packages with
pip
locally, there is rarely a repeated time cost becausepip
can tell when a package has already been installed and doesn't need to be re-installed. In order to accomplish similar behavior during image build, we would need a mechanism to persist the package installation directory between builds so thatpip
could reuse them instead of re-installing. I decided to try using build cache mounts to accomplish this.However, by installing all
pip
dependencies into a cache mount, they were no longer part of the image. To work around this, I chose to copy the virtual environment to a directory outside the cache mount at the end of theRUN
command that used it. This way, I could take advantage of the cache mount to reduce time spent installing dependencies, but keep a copy of the virtual environment in the image to be picked up by another stage in our multi-stage image.Solution
Simplified for readability
Using this approach, I was able to reduce image re-builds to 2-4 minutes, which would go a long way to improving our developer experience when cached image layers are invalidated.
Questions
I feel a bit uncomfortable about this solution because I haven't heard of other people caching python virtual environments or other package installation locations to improve build speed. Additionally, it doesn't really seem like the way this feature was intended to be used, though it seems to work well enough. What I'm wondering is:
Thank you very much in advance for any input/advice you might have!
Beta Was this translation helpful? Give feedback.
All reactions