File size: 1,427 Bytes
e0f8624 de85a22 c7b1ae2 f620980 60eeb4d e0f8624 60eeb4d c7b1ae2 e0f8624 ca87c3c aa5a346 60eeb4d f620980 60eeb4d f620980 de85a22 60eeb4d e0f8624 60eeb4d de85a22 e0f8624 de85a22 60eeb4d de85a22 e0f8624 60eeb4d e0f8624 ca87c3c 60eeb4d e0f8624 60eeb4d 3eec113 933a887 60eeb4d dd92e46 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# Stage 1: Builder
FROM python:3.10-slim AS builder
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
# Set the working directory
WORKDIR /app
# Copy the requirements file first for better caching
COPY requirements.txt /app/
# Install dependencies system-wide
RUN pip install --no-cache-dir --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Stage 2: Production
FROM python:3.10-slim
# Install system dependencies required for production
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Create a non-root user and group
RUN groupadd -r appuser && useradd -r -g appuser appuser
# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
# Set the working directory
WORKDIR /app
# Copy installed Python packages from the builder stage
COPY --from=builder /usr/local /usr/local
# Copy the current directory contents into the container
COPY . /app
# Change ownership to the non-root user
RUN chown -R appuser:appuser /app
# Switch to the non-root user
USER appuser
# Expose the port
EXPOSE 8001
# Run gunicorn
CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "--workers", "4", "--bind", "0.0.0.0:8001", "main:app"] |