2025-11-03 12:30:55 +02:00
|
|
|
# Use official Python runtime as base image - latest secure version
|
|
|
|
|
FROM python:3.12-slim-bookworm AS base
|
2025-09-29 09:31:37 +04:00
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Security: Set up non-root user first
|
|
|
|
|
RUN groupadd -g 1001 appuser && \
|
|
|
|
|
useradd -m -u 1001 -g appuser appuser
|
|
|
|
|
|
|
|
|
|
# Set environment variables for security and performance
|
2025-09-29 09:31:37 +04:00
|
|
|
ENV PYTHONUNBUFFERED=1 \
|
|
|
|
|
PYTHONDONTWRITEBYTECODE=1 \
|
2025-11-03 12:30:55 +02:00
|
|
|
PYTHONHASHSEED=random \
|
2025-09-29 09:31:37 +04:00
|
|
|
PIP_NO_CACHE_DIR=1 \
|
|
|
|
|
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
2025-11-03 12:30:55 +02:00
|
|
|
PIP_DEFAULT_TIMEOUT=100 \
|
|
|
|
|
PIP_ROOT_USER_ACTION=ignore \
|
|
|
|
|
DEBIAN_FRONTEND=noninteractive
|
|
|
|
|
|
|
|
|
|
# Install security updates and minimal dependencies
|
|
|
|
|
RUN apt-get update && \
|
|
|
|
|
apt-get upgrade -y && \
|
|
|
|
|
apt-get install -y --no-install-recommends \
|
2025-09-29 09:31:37 +04:00
|
|
|
ca-certificates \
|
2025-11-03 12:30:55 +02:00
|
|
|
&& apt-get clean \
|
|
|
|
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
|
2025-09-29 09:31:37 +04:00
|
|
|
&& update-ca-certificates
|
|
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Create app directory with correct permissions
|
2025-07-02 15:58:35 +02:00
|
|
|
WORKDIR /app
|
2025-11-03 12:30:55 +02:00
|
|
|
RUN chown -R appuser:appuser /app
|
2025-09-29 09:31:37 +04:00
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Copy requirements as root to ensure they're readable
|
|
|
|
|
COPY --chown=appuser:appuser requirements.txt .
|
2025-09-29 09:31:37 +04:00
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Install Python dependencies as root for system-wide access
|
|
|
|
|
RUN pip install --no-cache-dir --upgrade pip==24.3.1 && \
|
|
|
|
|
pip install --no-cache-dir -r requirements.txt
|
2025-09-29 09:31:37 +04:00
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Copy application code with correct ownership
|
|
|
|
|
COPY --chown=appuser:appuser . .
|
2025-09-29 09:31:37 +04:00
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Create necessary directories with correct permissions
|
|
|
|
|
RUN mkdir -p /app/database /app/workflows /app/static /app/src && \
|
2025-09-29 09:31:37 +04:00
|
|
|
chown -R appuser:appuser /app
|
|
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Security: Switch to non-root user
|
2025-09-29 09:31:37 +04:00
|
|
|
USER appuser
|
|
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Healthcheck
|
2025-09-29 09:31:37 +04:00
|
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
2025-11-03 12:30:55 +02:00
|
|
|
CMD python -c "import requests; requests.get('http://localhost:8000/api/stats')" || exit 1
|
2025-09-29 09:31:37 +04:00
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Expose port (informational)
|
2025-09-29 09:31:37 +04:00
|
|
|
EXPOSE 8000
|
|
|
|
|
|
2025-11-03 12:30:55 +02:00
|
|
|
# Security: Run with minimal privileges
|
|
|
|
|
CMD ["python", "-u", "run.py", "--host", "0.0.0.0", "--port", "8000"]
|