-
Notifications
You must be signed in to change notification settings - Fork 8.4k
chore: reduce image size by 33% #11664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
chore: reduce image size by 33% #11664
Conversation
WalkthroughModified Docker build configuration to optimize image size by adding Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 6 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (6 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🤖 Fix all issues with AI agents
In `@docker/build_and_push.Dockerfile`:
- Around line 141-157: The runtime-stage RUN that deletes files after COPY
--from=builder (the block starting with "RUN cd /app/.venv && \\" that removes
tests, docs, C/C++/Cython sources, strips .so, and rm -rf share/man) should be
moved into the builder stage so those files are never written into the layer
copied by COPY --from=builder; update the builder-stage cleanup (the existing
cleanup near the builder stage) to include the same find/rm and strip commands
for tests, __pycache__, docs (*.md, *.rst, *.txt), C/C++ sources (*.c, *.h,
*.cpp, *.hpp, *.cc), Cython files (*.pyx, *.pxd, *.pxi), and strip
--strip-unneeded on *.so, then delete the entire runtime-stage cleanup RUN block
(the post-COPY cleanup) so no whiteout layer is created.
- Line 149: The find command that currently deletes "*.txt" (the line containing
find . -type f \( -name "*.md" -o -name "*.rst" -o -name "*.txt" -o -name
"*.TXT" \) -delete) will remove critical metadata files inside .dist-info
directories; change the deletion to either remove *.txt from that pattern or
scope the find to exclude .dist-info (e.g., add a path exclusion for .dist-info
or use -not -path '*/.dist-info/*') so files like
entry_points.txt/top_level.txt/requires.txt are preserved while still deleting
doc files.
- Around line 73-76: The Dockerfile currently deletes __pycache__ directories in
the RUN step (the find . -type d -name "__pycache__" -exec rm -rf {} + command)
which negates UV_COMPILE_BYTECODE=1; either remove that __pycache__ deletion
from the RUN pipeline (and the duplicate __pycache__ deletion later in the file)
so precompiled .pyc files produced by UV_COMPILE_BYTECODE are preserved, or
alternatively remove the UV_COMPILE_BYTECODE=1 setting if you intentionally want
to discard bytecode to minimize image size—update the RUN line(s) that reference
"__pycache__" and the UV_COMPILE_BYTECODE variable accordingly.
- Around line 88-117: The RUN apt-get install block is using pre-Trixie package
names that will fail on python:3.12.12-slim-trixie; update the listed
Chromium/runtime packages in that RUN step by replacing libasound2 with
libasound2t64, libcups2 with libcups2t64, libatk1.0-0 with libatk1.0-0t64,
libatk-bridge2.0-0 with libatk-bridge2.0-0t64, and libatspi2.0-0 with
libatspi2.0-0t64 so apt-get can find the correct Debian Trixie packages (keep
the rest of the RUN block, comments and cleanup steps unchanged).
Signed-off-by: Sebastien NICOT <sebastien.nicot@enterprisedb.com>
ae3dd52 to
a02c83e
Compare
Optimize Docker Image Size - 40%+ Reduction
Summary
This PR significantly reduces the Docker image size from 7.75GB to ~4.5GB (40%+ reduction, saving ~3.25GB) through a series of optimizations while maintaining full application functionality.
Motivation
The current official Langflow Docker image (
langflowai/langflow:latest) is 7.75GB, which:Changes Made
1. Exclude Development Dependencies (~500-800MB saved)
Added
--no-devflag touv synccommands to exclude development packages (pytest, mypy, ruff, etc.) from the production image.2. Install Only Chromium Browser (~1.5GB saved)
Changed Playwright installation to only include Chromium instead of all browsers (Chromium, Firefox, and WebKit).
3. Aggressive Python Package Cleanup in Builder Stage (~500MB-1GB saved)
Critical optimization: All cleanup happens in the builder stage BEFORE copying to runtime. This ensures deleted files never make it into the final image layers.
Added comprehensive cleanup of unnecessary files from the virtual environment:
tests/,test/,.pytest_cache/directories*.md,*.rst,*.txtfiles*.c,*.h,*.cpp)*.pyx,*.pxd,*.pxifiles.sofiles usingstrip --strip-unneededshare/manandman/directoriesWhy this matters: Cleaning up in the runtime stage would only add whiteout markers without reducing image size. All cleanup must happen before the
COPYto actually reduce the final image size.4. Optimized Node.js Installation (~30-50MB saved)
5. Removed Unnecessary Runtime Dependencies
Removed git and gnupg from the runtime image (only needed during build).
Results
Size Breakdown Analysis
Top consumers after optimization:
Testing
Breaking Changes
None. All application features remain fully functional:
Technical Notes
Docker Layer Optimization
The most critical optimization is ensuring all cleanup happens in the builder stage before COPY:
Why Manual Chromium Dependencies?
We manually install Chromium dependencies instead of using
playwright install --with-depsbecause:--with-depsuses hardcoded package lists for Ubuntu 20.04ttf-unifont→fonts-unifont)Python Package Cleanup Safety
The cleanup is safe because: