uv: Cache invalidation on local dependencies when using docker
Hello.
I am having difficulties using uv in docker. My project structure has two python libraries, one is an API server, and the other is an internal library.
The problem I am encountering is that after building, when running the image, the uv run reinstalls my internal libraries, with the following message: Requirement installed, but not fresh: .... This is problematic, because as per docker best practices, it is good to ensure that the install actually happens on the build stage, and not in the actual running phase of my application.
When debugging the issue further, I found out that it happens because the ctime of my pyproject.toml of the internal library changes between the docker build and the actual run.
I finally validated that symptom using the following dockerfile:
FROM ubuntu:22.04
RUN touch a && stat a > /root/stat-a
RUN sleep 5
And running: docker build --no-cache . -t playground && docker run playground sh -c 'cat /root/stat-a && stat a'
The output that I received was:
File: a
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: e3h/227d Inode: 1336246 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-11-18 10:04:52.462498005 +0000
Modify: 2024-11-18 10:04:52.462498005 +0000
Change: 2024-11-18 10:04:52.462498005 +0000
Birth: 2024-11-18 10:04:52.462498005 +0000
---
File: a
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: e3h/227d Inode: 225316 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-11-18 10:04:52.000000000 +0000
Modify: 2024-11-18 10:04:52.000000000 +0000
Change: 2024-11-18 10:04:57.680498008 +0000
Birth: 2024-11-18 10:04:57.680498008 +0000
As seen, two things are changing between the build and run - both the inode number, and the Change & Birth times. This seems like an odd behavior of docker, but as I dived deeper it seems inherent to the layering system of docker.
I think that the solution to that issue should come from uv’s side, as the usage for similar type of projects in docker should be very common.
A possible approach that would solve both the cache freshness problem and would work with docker’s odd behavior would be using checksums for local dependencies freshness check.
Some additional information:
- uv version: v0.5.1
- Platform: MacOS with Docker Desktop
Thanks in advance! Yannay
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 7
- Comments: 18 (9 by maintainers)
Same issue on my side - following this example leads to
Uninstalled 1 package [..] Installed 1 package. Using --verbose on uv run surfaces “Requirement installed, but not fresh”, with the requirement being the project itself.