Skip to content

Commit ae3dd52

Browse files
committed
chore: reduce image size by 33%
Signed-off-by: Sebastien NICOT <sebastien.nicot@enterprisedb.com>
1 parent 6ed0091 commit ae3dd52

1 file changed

Lines changed: 70 additions & 8 deletions

File tree

docker/build_and_push.Dockerfile

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ COPY ./src/lfx/pyproject.toml /app/src/lfx/pyproject.toml
5151

5252
RUN --mount=type=cache,target=/root/.cache/uv \
5353
RUSTFLAGS='--cfg reqwest_unstable' \
54-
uv sync --frozen --no-install-project --no-editable --extra postgresql
54+
uv sync --frozen --no-install-project --no-editable --no-dev --extra postgresql
5555

5656
COPY ./src /app/src
5757

@@ -67,20 +67,57 @@ WORKDIR /app
6767

6868
RUN --mount=type=cache,target=/root/.cache/uv \
6969
RUSTFLAGS='--cfg reqwest_unstable' \
70-
uv sync --frozen --no-editable --extra postgresql
70+
uv sync --frozen --no-editable --no-dev --extra postgresql
71+
72+
# Clean up build artifacts and unnecessary files before copying to runtime
73+
RUN cd /app/.venv && \
74+
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
75+
find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" \) -delete 2>/dev/null || true && \
76+
find . -name "*.so" -exec strip --strip-unneeded {} \; 2>/dev/null || true
7177

7278
################################
7379
# RUNTIME
7480
# Setup user, utilities and copy the virtual environment only
7581
################################
7682
FROM python:3.12.12-slim-trixie AS runtime
7783

78-
84+
# Install only essential runtime dependencies:
85+
# - libpq5: PostgreSQL client library (for database connections)
86+
# - curl: Required for Node.js installation
87+
# - Chromium dependencies for Playwright (minimal set for headless operation)
7988
RUN apt-get update \
8089
&& apt-get upgrade -y \
81-
&& apt-get install --no-install-recommends -y curl git libpq5 gnupg xz-utils \
90+
&& apt-get install --no-install-recommends -y \
91+
libpq5 \
92+
curl \
93+
xz-utils \
94+
# Chromium dependencies
95+
libnss3 \
96+
libnspr4 \
97+
libatk1.0-0 \
98+
libatk-bridge2.0-0 \
99+
libcups2 \
100+
libdrm2 \
101+
libdbus-1-3 \
102+
libxkbcommon0 \
103+
libxcomposite1 \
104+
libxdamage1 \
105+
libxfixes3 \
106+
libxrandr2 \
107+
libgbm1 \
108+
libpango-1.0-0 \
109+
libcairo2 \
110+
libasound2 \
111+
libatspi2.0-0 \
112+
libxshmfence1 \
113+
# Fonts (using Debian Trixie package names)
114+
fonts-liberation \
115+
fonts-noto-color-emoji \
82116
&& apt-get clean \
83117
&& rm -rf /var/lib/apt/lists/*
118+
119+
# Install Node.js (needed for MCP servers that use npx)
120+
# Use minimal installation: download pre-built binaries directly
84121
RUN ARCH=$(dpkg --print-architecture) \
85122
&& if [ "$ARCH" = "amd64" ]; then NODE_ARCH="x64"; \
86123
elif [ "$ARCH" = "arm64" ]; then NODE_ARCH="arm64"; \
@@ -90,14 +127,39 @@ RUN ARCH=$(dpkg --print-architecture) \
90127
| head -1) \
91128
&& curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${NODE_ARCH}.tar.xz" \
92129
| tar -xJ -C /usr/local --strip-components=1 \
93-
&& npm install -g npm@latest \
94-
&& npm cache clean --force
130+
&& npm cache clean --force \
131+
&& rm -rf /usr/local/lib/node_modules/npm/docs \
132+
&& rm -rf /usr/local/lib/node_modules/npm/man
133+
95134
RUN useradd user -u 1000 -g 0 --no-create-home --home-dir /app/data
96135

97136
COPY --from=builder --chown=1000 /app/.venv /app/.venv
98137
ENV PATH="/app/.venv/bin:$PATH"
99-
RUN /app/.venv/bin/pip install --upgrade playwright \
100-
&& /app/.venv/bin/playwright install
138+
139+
# Clean up unnecessary files from virtual environment to reduce image size
140+
# This can save 500MB-1GB
141+
RUN cd /app/.venv && \
142+
# Remove test directories and files
143+
find . -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
144+
find . -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \
145+
find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true && \
146+
# Remove __pycache__ directories (redundant with .pyc files in site-packages)
147+
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
148+
# Remove documentation files
149+
find . -type f \( -name "*.md" -o -name "*.rst" -o -name "*.txt" -o -name "*.TXT" \) -delete 2>/dev/null || true && \
150+
# Remove C/C++ source and headers (not needed after compilation)
151+
find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.cc" \) -delete 2>/dev/null || true && \
152+
# Remove Cython source files
153+
find . -type f \( -name "*.pyx" -o -name "*.pxd" -o -name "*.pxi" \) -delete 2>/dev/null || true && \
154+
# Strip debug symbols from shared libraries
155+
find . -name "*.so" -exec strip --strip-unneeded {} \; 2>/dev/null || true && \
156+
# Remove man pages and docs
157+
rm -rf share/man man 2>/dev/null || true
158+
159+
# Install only Chromium browser for Playwright (not all browsers)
160+
# This saves ~1.5GB compared to installing all browsers
161+
# Install without --with-deps since we manually installed dependencies above
162+
RUN /app/.venv/bin/playwright install chromium
101163

102164
LABEL org.opencontainers.image.title=langflow
103165
LABEL org.opencontainers.image.authors=['Langflow']

0 commit comments

Comments
 (0)