FROM python:3.11-slim

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    postgresql-client \
    curl \
    netcat-traditional \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user
RUN useradd -m -u 1000 celery

# Set working directory
WORKDIR /app

# Copy requirements first to leverage Docker cache
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Create necessary directories and set permissions
RUN mkdir -p /app/uploads/rooms /app/uploads/profile_pics /app/static/uploads && \
    chown -R celery:celery /app && \
    chmod -R 755 /app/uploads

# Make entrypoint script executable
RUN chmod +x /app/entrypoint.sh && \
    chown celery:celery /app/entrypoint.sh

# Switch to non-root user
USER celery

# Set entrypoint
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"] 