Background
In PR #342, a multi-stage Dockerfile was introduced for the FastAPI application. The current implementation duplicates the pip install step in both the build and runtime stages.
Suggested Improvement
Leverage the build stage to prepare wheels, then install from that wheelhouse in the runtime stage to speed up builds and reduce image size.
Proposed Changes
# In the build stage:
-COPY requirements.txt .
-RUN pip install --no-cache-dir -r requirements.txt
+COPY requirements.txt .
+RUN pip wheel --no-cache --no-deps -r requirements.txt -w /app/wheelhouse
# In the runtime stage:
-COPY requirements.txt .
-RUN pip install --no-cache-dir -r requirements.txt
+COPY requirements.txt .
+COPY --from=build /app/wheelhouse /app/wheelhouse
+RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt
Benefits
- Faster build times
- Reduced image size
- Better caching in CI/CD pipelines
References
Background
In PR #342, a multi-stage Dockerfile was introduced for the FastAPI application. The current implementation duplicates the
pip installstep in both the build and runtime stages.Suggested Improvement
Leverage the build stage to prepare wheels, then install from that wheelhouse in the runtime stage to speed up builds and reduce image size.
Proposed Changes
Benefits
References