From d2a7f17752d8c899b7e071c97456a79f5a50fc5e Mon Sep 17 00:00:00 2001 From: johnpccd Date: Fri, 23 May 2025 22:57:11 +0200 Subject: [PATCH] dockerfile: 2-stage --- Dockerfile | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 84c6e0a..2d33dda 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,9 @@ -FROM python:3.9-slim +# Build stage +FROM python:3.9-slim AS builder WORKDIR /app -# Install system dependencies +# Install build dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && apt-get clean \ @@ -11,17 +12,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ # Copy requirements file COPY requirements.txt . -# Install Python dependencies -RUN pip install --no-cache-dir -r requirements.txt +# Install dependencies into a virtual environment +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir -r requirements.txt -# Copy application code -COPY . . +# Final stage +FROM python:3.9-slim + +WORKDIR /app + +# Copy only the necessary files from the builder stage +COPY --from=builder /opt/venv /opt/venv + +# Make sure we use the virtual environment +ENV PATH="/opt/venv/bin:$PATH" +ENV PYTHONUNBUFFERED=1 + +# Copy only the application code needed to run the service +COPY src/ ./src/ +COPY main.py . # Expose the port the app runs on EXPOSE 8000 -# Set environment variables -ENV PYTHONUNBUFFERED=1 - # Command to run the application CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file