From 783c8a42d9bff3570b8407b3181d269b21b875fc Mon Sep 17 00:00:00 2001 From: Stanislav Hejny Date: Wed, 2 Jul 2025 08:46:47 +0100 Subject: [PATCH] build: optimize Dockerfile with multi-stage build Co-authored-by: aider (openrouter/openai/o3-mini-high) --- Dockerfile | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index ffe5122..8af4c28 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,29 @@ -# Use the official Python image as the base image -FROM python:3.11-slim - -# Set the working directory to /app +# Build stage +FROM python:3.11-slim AS builder WORKDIR /app ENV PYTHONPATH=/app -# Install system dependencies -RUN apt-get update && apt-get install -y \ - gcc \ - && rm -rf /var/lib/apt/lists/* +# Install system dependencies for building +RUN apt-get update && apt-get install -y gcc && rm -rf /var/lib/apt/lists/* -# Copy requirements and install Python dependencies +# Copy project files for building COPY pyproject.toml . -RUN pip install --no-cache-dir build -RUN pip install --no-cache-dir . +COPY . . -# Install additional dependencies for demo.py +RUN pip install --no-cache-dir build && python -m build --wheel + +# Runtime stage +FROM python:3.11-slim +WORKDIR /app +ENV PYTHONPATH=/app + +RUN apt-get update && apt-get install -y libpq-dev && rm -rf /var/lib/apt/lists/* + +# Install the built package from builder +COPY --from=builder /app/dist/*.whl /tmp/ +RUN pip install --no-cache-dir /tmp/*.whl + +# Install additional runtime dependencies for demo.py RUN pip install --no-cache-dir \ uvicorn[standard]==0.29.0 \ python-multipart==0.0.20 \ @@ -28,7 +36,7 @@ RUN pip install --no-cache-dir \ opentelemetry-instrumentation-httpx==0.55b1 \ protobuf==5.28.3 -# Copy the application code +# Copy the application code if needed COPY ./amqp ./amqp COPY ./otdemo ./otdemo COPY ./demo.py . @@ -36,8 +44,6 @@ COPY ./demo.py . # Create directory for uploaded files RUN mkdir -p /tmp/clevermicro_documents -# Expose the port EXPOSE 8000 8001 -# Run the FastAPI application CMD ["uvicorn", "demo:app", "--host", "0.0.0.0", "--port", "8000"]