Skip to content

Optimize Dockerfile: Avoid duplicating pip install across stages #345

@coderabbitai

Description

@coderabbitai

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

Metadata

Metadata

Assignees

Labels

containersPull requests that update containers codeenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions