Dockerfile: add multi-stage build, pin uv, improve caching and runtime user handling - #19
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Dockerfile to enhance the build process and runtime environment. The changes aim to improve build reproducibility, reduce the final image size by separating build and runtime concerns, and bolster security by ensuring the application runs as an unprivileged user. These modifications lead to more efficient and secure container images. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR refactors the Dockerfile to use a multi-stage build with Python 3.12.10-slim, pinning uv to version 0.5.26, moving dependency resolution into a builder stage, and creating an unprivileged app user with optimized layer caching and explicit file ownership configuration. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request significantly improves the Docker setup by introducing a multi-stage build, which enhances reproducibility, security, and build performance. Pinning versions for both the base image and uv is a great step towards reproducible builds. The use of a non-root user and --chown in the COPY instruction are excellent security and optimization practices. I've added a couple of suggestions to further reduce image layers and improve the Dockerfile's maintainability.
Please also consider updating the documentation in docs/deployment/docker-and-github-actions.md to reflect the new multi-stage build process. Specifically, the docker build command now requires --target runtime to build the final image.
| FROM python:3.12-slim | ||
| # syntax=docker/dockerfile:1.4 | ||
|
|
||
| FROM python:3.12.10-slim AS builder |
There was a problem hiding this comment.
To improve maintainability and reduce duplication, you could introduce a base stage for common setup like the base image, ENV variables, and WORKDIR. Both builder and runtime stages can then inherit from this base stage. This means you'd only need to update these common settings in one place.
For example:
# syntax=docker/dockerfile:1.4
FROM python:3.12.10-slim AS base
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
WORKDIR /app
FROM base AS builder
# ... rest of builder stage, removing ENV and WORKDIR
FROM base AS runtime
ENV PATH="/app/.venv/bin:$PATH"
# ... rest of runtime stage, removing the other ENV vars and WORKDIR| COPY --from=builder --chown=app:app /app/.venv /app/.venv | ||
| COPY --from=builder --chown=app:app /app/src /app/src | ||
| COPY --from=builder --chown=app:app /app/data /app/data |
There was a problem hiding this comment.
You can combine these three COPY instructions into a single one to reduce the number of layers in the final image. This aligns well with the PR's goal of optimizing layers.
To do this, you would first collect all runtime artifacts into a single directory in the builder stage, for example, by adding this step after the final uv sync (line 27):
# In builder stage
RUN mkdir /artifacts && cp -r .venv src data /artifactsThen, you can replace these three COPY lines with a single one:
COPY --from=builder --chown=app:app /artifacts/. /app/
Motivation
chownpasses.Description
FROM python:3.12.10-slim AS builderand a separateruntimestage.uvtoghcr.io/astral-sh/uv:0.5.26and copy only the dependency manifestspyproject.tomlanduv.lockto maximize layer cache effectiveness.uv sync --frozen --no-cache --no-install-projectto install dependencies first, then copy source and data and runuv sync --frozen --no-cacheto install the local project.appuser in the runtime stage and copy runtime artifacts from the builder with--chown=app:appto avoid an extrachown -Rlayer, setPATH="/app/.venv/bin:$PATH", addEXPOSE 80, and run the app via thefastapibinary onPATH.Testing
docker build --target runtime -t acebet:pr .and the build completed successfully.docker run --rm acebet:pr fastapi --helpand thefastapibinary executed successfully, confirming the virtualenv is onPATH.Codex Task
Summary by CodeRabbit