11FROM astral/uv:python3.11-trixie-slim AS builder
22
3- # Install build dependencies including Rust for packages that need it
4- RUN apt-get update && apt-get install -y \
3+ # Install build dependencies with minimal footprint
4+ RUN apt-get update && apt-get install -y --no-install-recommends \
55 build-essential \
66 git \
77 curl \
88 pkg-config \
99 libssl-dev \
10- && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
10+ && rm -rf /var/lib/apt/lists/* \
11+ && apt-get autoremove -y
12+
13+ # Install Rust with minimal profile and immediate cleanup
14+ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal \
1115 && . ~/.cargo/env \
12- && rm -rf /var/lib/apt/lists/*
16+ && rustup component add rustfmt
1317
1418# Add Rust to PATH
1519ENV PATH="/root/.cargo/bin:${PATH}"
@@ -23,24 +27,50 @@ COPY pyproject.toml uv.lock ./
2327ENV UV_EXTRA_INDEX_URL="https://download.pytorch.org/whl/cpu"
2428ENV TORCH_INDEX_URL="https://download.pytorch.org/whl/cpu"
2529
26- # Install dependencies using uv with proper build environment
30+ # Install dependencies with aggressive progressive cleanup
2731RUN . ~/.cargo/env && \
28- uv sync --frozen --no-dev --no-cache && \
29- # Clean up any temporary files to reduce layer size
30- rm -rf /root/.cache/uv /tmp/* /var/tmp/* && \
31- # Remove Rust toolchain after build to reduce image size
32- rustup self uninstall -y
32+ # Install dependencies with bytecode compilation for better performance
33+ uv sync --frozen --no-dev --no-cache --compile-bytecode && \
34+ # Immediate cleanup of build artifacts during installation
35+ find /app/.venv -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
36+ find /app/.venv -name "*.pyc" -delete 2>/dev/null || true && \
37+ find /app/.venv -name "*.pyo" -delete 2>/dev/null || true && \
38+ # Remove test files and documentation from packages (keeping runtime libs)
39+ find /app/.venv -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
40+ find /app/.venv -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \
41+ find /app/.venv -type d -name "docs" -exec rm -rf {} + 2>/dev/null || true && \
42+ # Strip debug symbols from shared libraries to reduce size
43+ find /app/.venv -name "*.so" -exec strip {} + 2>/dev/null || true && \
44+ # Aggressive cache and temporary file cleanup
45+ rm -rf /root/.cache/uv \
46+ /root/.cache/pip \
47+ /root/.cache/* \
48+ /tmp/* \
49+ /var/tmp/* \
50+ /root/.cargo/registry \
51+ /root/.cargo/git \
52+ /app/.venv/share \
53+ && \
54+ # Remove Rust toolchain completely after build
55+ rustup self uninstall -y && \
56+ # Final build tools cleanup to free space
57+ apt-get autoremove -y build-essential git curl pkg-config && \
58+ apt-get autoclean
3359
3460# ----------------------------------------
3561
3662FROM python:3.11-slim-trixie AS runtime
3763
38- # Install only runtime dependencies
39- RUN apt-get update && apt-get install -y \
64+ # Install minimal runtime dependencies
65+ RUN apt-get update && apt-get install -y --no-install-recommends \
4066 libssl3 \
4167 libffi8 \
68+ # Add required libraries for ML packages
69+ libgomp1 \
70+ libglib2.0-0 \
4271 && rm -rf /var/lib/apt/lists/* \
43- && apt-get clean
72+ && apt-get autoremove -y \
73+ && apt-get autoclean
4474
4575WORKDIR /app
4676
@@ -50,7 +80,7 @@ COPY --from=builder /app/.venv /app/.venv
5080# Copy dependency files
5181COPY pyproject.toml uv.lock ./
5282
53- # Copy the rest of the application
83+ # Copy the application
5484COPY . .
5585
5686# Make sure we use the virtual environment
0 commit comments