cuddly-jelly-27016
04/12/2025, 2:03 PM_F_IMG_ID env var all the way at the bottom during ImageSpec builds
Pull request opened by redartera
## Why are the changes needed?
Docker build caching is highly sensitive to the order of operations in a Dockerfile. Currently, the _F_IMG_ID environment variable (which contains a hash of the ImageSpec dataclass) is set near the top of the generated Dockerfile. When this value changes due to modifications in the ImageSpec, it invalidates the cache for that layer and all subsequent layers, causing unnecessary rebuilds of the Python environment even when those layers haven't changed functionally (e.g. python dependencies have remained the same but something changed in the commands or copy block following it)
## What changes were proposed in this pull request?
This PR moves the _F_IMG_ID environment variable declaration to the bottom of the generated Dockerfile, after all major build steps (like uv/poetry/pip env installations). This placement ensures that changes to the ImageSpec as a whole only invalidate the layers that are directly affected, preventing costly rebuilds of the Python environment when only commands or other non-environment components change.
## How was this patch tested?
The change was tested locally by modifying flytekit and observing Docker build behavior. When the _F_IMG_ID is placed at the bottom of the Dockerfile, changes to components like commands only invalidate the cache for those specific layers and what follows them, preserving the cache for expensive operations like Poetry environment setup.
This improvement reduces build times and registry storage space by maintaining slightly better cache consistency, especially for iterative development workflows where small changes shouldn't trigger full environment rebuilds.
### Summary by Bito
This PR optimizes Dockerfile generation by repositioning the _F_IMG_ID environment variable to the end of the build process. This strategic change preserves cache layers for steps unaffected by ImageSpec changes, preventing unnecessary rebuilds when only non-critical configuration changes occur. This significantly improves build times, reduces storage requirements, and enhances overall Docker build performance during development.
Unit tests added: False
Estimated effort to review (1-5, lower is better): 1
flyteorg/flytekitcuddly-jelly-27016
05/06/2025, 7:18 PM