Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Stage 1: Build
FROM python:3.12-slim-bookworm AS build

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# Stage 2: Runtime
FROM python:3.12-slim-bookworm AS runtime

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

Comment on lines +11 to +18
Copy link

@coderabbitai coderabbitai bot Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid duplicating pip install across stages
Both stages install from requirements.txt. Leverage the build stage to prepare wheels, then install from that wheelhouse in runtime to speed up builds and reduce image size.

# 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

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open a follow-up GitHub issue for this discussion.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

@nanotaboada I've created a follow-up GitHub issue to track the Dockerfile optimization we discussed. The issue includes the detailed explanation of how to avoid duplicating pip install across build stages by using wheels to speed up builds and reduce image size.

The issue has been assigned to you for future implementation when you're ready to address it.


🏁 Script executed:

#!/bin/bash
# Create a GitHub issue for the Dockerfile optimization suggestion

gh issue create \
  --title "Optimize Dockerfile: Avoid duplicating pip install across stages" \
  --body "## 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

\`\`\`diff
# 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
- PR: https://github.com/nanotaboada/python-samples-fastapi-restful/pull/342
- Discussion: https://github.com/nanotaboada/python-samples-fastapi-restful/pull/342#discussion_r2055005387
" \
  --assignee "nanotaboada"

# Check if the issue was created successfully
if [ $? -eq 0 ]; then
  echo "Issue created successfully."
else
  echo "Failed to create issue. Error code: $?"
fi

Length of output: 1439


@nanotaboada Great! The issue has been successfully created as #345. You can access it directly at: #345

The issue includes all the details about optimizing the Dockerfile by avoiding duplication of the pip install step across stages, along with the code examples and benefits.

🐰✨


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

COPY models ./models
COPY routes ./routes
COPY schemas ./schemas
COPY services ./services
COPY data ./data
COPY main.py .

# Add non-root 'fastapi' user (optional for hardening)
RUN adduser --disabled-password --gecos '' fastapi \
&& chown -R fastapi:fastapi /app
USER fastapi

EXPOSE 9000
ENV PYTHONUNBUFFERED=1

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]
16 changes: 16 additions & 0 deletions commitlint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// .commitlint.config.mjs
import conventional from '@commitlint/config-conventional';

export default {
...conventional,
rules: {
'header-max-length': [2, 'always', 80],
'body-max-line-length': [2, 'always', 80],
},
ignores: [
// skip any commit whose body contains the Dependabot signature
(message) => message.includes('Signed‑off‑by: dependabot[bot]'),
// skip any Dependabot‑style bump header
(message) => /^chore\(deps(-dev)?\): bump /.test(message),
],
};
Loading