chore: optimize Dockerfile for quicker builds#4418
Conversation
- Added a new build stage for collecting Django static files in parallel with the frontend build. - Updated file copying commands to set ownership for the application files.
📝 WalkthroughWalkthroughRestructures the Dockerfile to add a Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Dev as "Dev / CI"
participant Frontend as "frontend build stage"
participant BackendStatic as "backend_static (collectstatic)"
participant Final as "final image stage"
Dev->>Frontend: start frontend build (assets)
Dev->>BackendStatic: run Django collectstatic (pre-collect static)
Frontend-->>Final: copy built frontend assets (--chown=1001:0)
BackendStatic-->>Final: copy collected static files (--chown=1001:0)
Dev->>Final: copy source & deps with --chown=1001:0 and switch to USER 1001
Note right of Final: Final image contains pre-built assets and non-root user
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 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.
🧹 Nitpick comments (1)
Dockerfile (1)
58-58: Consider copying only the files needed forcollectstaticto improve cache hit rates.
COPY . /app/pulls in the entire build context (including frontend source, docs, etc.), so any file change — even a frontend-only change — busts the cache for this stage. Since the goal is only to runcollectstatic, you could narrow the copy to Python/Django files. That said, since this stage runs in parallel with the frontend build, the wall-clock impact is minimal.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@Dockerfile` at line 58, The Dockerfile currently uses COPY . /app/ which pulls the whole build context and busts cache for the collectstatic stage; replace this with a minimal copy of only the files needed to run collectstatic (e.g., COPY requirements.txt /app/ and install deps, then COPY manage.py, your Django package folders (e.g., myapp/, settings/, urls/), templates/ and static/ into /app/) or add a .dockerignore to exclude frontend/, docs/, node_modules/; update the Dockerfile stage that runs collectstatic (the step referencing COPY . /app/) to use those targeted COPY lines so cache is preserved and collectstatic runs with only Python/Django sources.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@Dockerfile`:
- Line 58: The Dockerfile currently uses COPY . /app/ which pulls the whole
build context and busts cache for the collectstatic stage; replace this with a
minimal copy of only the files needed to run collectstatic (e.g., COPY
requirements.txt /app/ and install deps, then COPY manage.py, your Django
package folders (e.g., myapp/, settings/, urls/), templates/ and static/ into
/app/) or add a .dockerignore to exclude frontend/, docs/, node_modules/; update
the Dockerfile stage that runs collectstatic (the step referencing COPY . /app/)
to use those targeted COPY lines so cache is preserved and collectstatic runs
with only Python/Django sources.
🚀 Preview EnvironmentYour preview environment is ready!
Details
ℹ️ Preview Environment InfoIsolation:
Limitations:
Cleanup:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
Dockerfile (1)
58-58: Consider scoping theCOPYto backend-only files for more efficient layering.While your
.dockerignoreproperly excludes sensitive files like.env, the fullCOPY . /app/still pulls unnecessary files (frontend assets, tests, documentation, etc.) into this intermediate stage where only the Django backend is needed forcollectstatic. Scoping this copy reduces intermediate layer size and follows the pattern already used in thefrontend_buildstage (COPY front_end/ /app/front_end/).♻️ Proposed scoped COPY
-COPY . /app/ +COPY manage.py pyproject.toml poetry.lock /app/ +COPY <your_django_app_dir>/ /app/<your_django_app_dir>/🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@Dockerfile` at line 58, Replace the broad "COPY . /app/" in the Dockerfile with a scoped copy that only brings backend/Django sources and static assets needed for collectstatic into the build stage (e.g., copy the backend app directory, requirements, manage.py, templates, and static folders), mirroring the pattern used in the frontend_build stage's "COPY front_end/ /app/front_end/"; update the stage that runs collectstatic to use those backend-only paths so intermediate layers are smaller and frontend/tests/docs are excluded from this stage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@Dockerfile`:
- Line 58: Replace the broad "COPY . /app/" in the Dockerfile with a scoped copy
that only brings backend/Django sources and static assets needed for
collectstatic into the build stage (e.g., copy the backend app directory,
requirements, manage.py, templates, and static folders), mirroring the pattern
used in the frontend_build stage's "COPY front_end/ /app/front_end/"; update the
stage that runs collectstatic to use those backend-only paths so intermediate
layers are smaller and frontend/tests/docs are excluded from this stage.
Summary by CodeRabbit