# Multi-stage Dockerfile for Django production deployment # Build stage 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 RUN apt-get update && apt-get install -y \ build-essential \ libpq-dev \ curl \ && curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* # Create and set work directory WORKDIR /app # Install Python dependencies COPY requirements/production.txt ./requirements/ RUN pip install --no-cache-dir -r requirements/production.txt # Production stage FROM python:3.11-slim # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ DJANGO_SETTINGS_MODULE=django_project.settings.production # Install system dependencies for production RUN apt-get update && apt-get install -y \ libpq5 \ curl \ && curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* # Create non-root user RUN groupadd -r django && useradd -r -g django django # Set work directory WORKDIR /app # Copy Python dependencies from builder stage COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages COPY --from=builder /usr/local/bin /usr/local/bin # Copy project files COPY . . # Install Tailwind and build CSS RUN DJANGO_SETTINGS_MODULE=django_project.settings.build python manage.py tailwind install RUN DJANGO_SETTINGS_MODULE=django_project.settings.build python manage.py tailwind build # Create static and media directories RUN mkdir -p staticfiles media # Collect static files RUN python manage.py collectstatic --noinput # Change ownership to django user RUN chown -R django:django /app # Switch to non-root user USER django # Expose port EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD python -c "import requests; requests.get('http://localhost:8000/health/', timeout=10)" || exit 1 # Start server CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "--timeout", "120", "django_project.wsgi:application"]