dockerfile: 2-stage

This commit is contained in:
johnpccd 2025-05-23 22:57:11 +02:00
parent f1c41a074d
commit d2a7f17752

View File

@ -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"]