# Multi-stage Dockerfile for Django application # Stage 1: Builder stage - Install dependencies FROM python:3.11-slim as builder # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 # Install system dependencies required for building Python packages RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ postgresql-client \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # Create and set working directory WORKDIR /app # Copy requirements file COPY requirements.txt . # Install Python dependencies RUN pip install --upgrade pip && \ pip install --user -r requirements.txt # Stage 2: Production stage - Minimal runtime image FROM python:3.11-slim # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PATH="/home/django/.local/bin:$PATH" # Install runtime dependencies only RUN apt-get update && apt-get install -y --no-install-recommends \ postgresql-client \ libpq5 \ bash \ && rm -rf /var/lib/apt/lists/* # Create non-root user for security RUN groupadd -r django && useradd -r -g django django # Create necessary directories RUN mkdir -p /app/staticfiles /app/media && \ chown -R django:django /app # Set working directory WORKDIR /app # Copy Python dependencies from builder stage COPY --from=builder /root/.local /home/django/.local # Copy project files COPY --chown=django:django . . # Copy and set permissions for entrypoint script COPY --chown=django:django entrypoint.sh /app/entrypoint.sh RUN chmod +x /app/entrypoint.sh # Switch to non-root user USER django # Expose port EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/')" || exit 1 # Use entrypoint script ENTRYPOINT ["/app/entrypoint.sh"] # Default command (can be overridden in docker-compose) CMD ["gunicorn", "zonco_backend.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3", "--timeout", "120"]