# VulnManager Dockerfile
FROM python:3.11-slim

# Security: Run as non-root user
RUN useradd -m -u 1000 vulnmanager

WORKDIR /app

# Install system dependencies.
# - postgresql-client: pg_isready in the entrypoint
# - dos2unix: entrypoint line-endings
# No -dev packages: every requirement ships a wheel (xmlsec and lxml bundle
# their own libxml2), and the headers only added CVE findings to the image
# (linux-libc-dev, libunbound, libxml2). upgrade: the base image lags behind
# Debian security updates.
RUN apt-get update && apt-get upgrade -y \
    && apt-get install -y --no-install-recommends \
    postgresql-client \
    dos2unix \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements and install Python dependencies. --only-binary: a
# requirement without a wheel fails the build instead of needing a compiler.
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
    && pip install --no-cache-dir --only-binary=:all: -r requirements.txt

# Copy application code (and entrypoint)
COPY . .

# Set permissions for entrypoint
RUN dos2unix /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
RUN chown -R vulnmanager:vulnmanager /app

# Switch to non-root user
USER vulnmanager

# Env vars defaults
ENV POSTGRES_HOST=postgres
ENV POSTGRES_USER=vulnmanager
ENV POSTGRES_DB=vulnmanager

# Expose port
EXPOSE 8000

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"

# Entrypoint
ENTRYPOINT ["/app/entrypoint.sh"]

# Start application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
