test24 / Dockerfile
Niansuh's picture
Update Dockerfile
60eeb4d verified
raw
history blame
1.43 kB
# 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"]