# --------------------------- # Builder stage # --------------------------- FROM python:3.11-slim AS builder ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 WORKDIR /app # Solo para compilar dependencias RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* # Copiamos archivos de dependencias primero (mejor cache) COPY pyproject.toml ./ # Creamos wheels (más limpio para producción) #RUN pip install --upgrade pip && \ # pip wheel --no-cache-dir --no-deps --wheel-dir /wheels . RUN pip install --upgrade pip && \ pip install --prefix=/install --no-warn-script-location . RUN find /install -type d -name '__pycache__' -exec rm -rf {} + && \ find /install -type f -name '*.pyc' -delete && \ find /install -type f -name '*.pyo' -delete # --------------------------- # Runtime stage # --------------------------- FROM python:3.11-alpine ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 ENV PYTHONPATH=/usr/local WORKDIR /app # Crear usuario no-root (mejor seguridad) #RUN useradd --create-home appuser # Copiar wheels generados #COPY --from=builder /wheels /wheels # Instalar dependencias sin herramientas de compilación #RUN pip install --upgrade pip && \ pip install --no-cache-dir /wheels/* && \ rm -rf /wheels # Copiar código #COPY . . # Cambiar propietario RUN apk add --no-cache libstdc++ libgcc && \ adduser -D -h /app appuser COPY --from=builder /install /usr/local RUN chown -R appuser:appuser /app USER appuser ENV BASIC_AUTH_USERNAME=admin ENV BASIC_AUTH_PASSWORD=admin EXPOSE 8000