79 lines
2.5 KiB
Docker
79 lines
2.5 KiB
Docker
# Multi-stage build for optimized Python application
|
|
FROM python:3.11-slim AS builder
|
|
|
|
# Set build-time environment variables
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PIP_NO_CACHE_DIR=1 \
|
|
PIP_DISABLE_PIP_VERSION_CHECK=1
|
|
|
|
# Install build dependencies only
|
|
RUN apt-get update && apt-get install -y \
|
|
build-essential \
|
|
curl \
|
|
git \
|
|
&& rm -rf /var/lib/apt/lists/* && \
|
|
apt-get clean
|
|
|
|
# Configure SSH to trust GitHub host key
|
|
RUN mkdir -p ~/.ssh && \
|
|
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts && \
|
|
chmod 600 ~/.ssh/known_hosts
|
|
|
|
# Create virtual environment
|
|
RUN python -m venv /opt/venv
|
|
ENV PATH="/opt/venv/bin:$PATH"
|
|
|
|
# Upgrade pip and wheel for better caching
|
|
RUN pip install --upgrade pip setuptools wheel
|
|
|
|
# Copy requirements files for better Docker layer caching
|
|
COPY requirements.txt ./
|
|
|
|
# Install only production dependencies with no cache
|
|
RUN --mount=type=ssh echo "=== Installing dependencies ===" && \
|
|
pip install --no-cache-dir -r requirements.txt && \
|
|
echo "=== Dependencies installed successfully ===" && \
|
|
pip list | wc -l && \
|
|
echo "=== Cleaning cache files ===" && \
|
|
find /opt/venv -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true && \
|
|
find /opt/venv -name "*.pyc" -delete 2>/dev/null || true && \
|
|
rm -rf /root/.cache/pip/* && \
|
|
echo "=== Cleaning venv site-packages ===" && \
|
|
find /opt/venv/lib/python3.11/site-packages/ -type f -name "*.md" -delete 2>/dev/null || true && \
|
|
echo "=== Stripping .so files ===" && \
|
|
find /opt/venv -name "*.so" -exec strip {} + 2>/dev/null || true
|
|
|
|
# Production stage using python-slim for better functionality
|
|
FROM python:3.11-slim AS production
|
|
|
|
# Set environment variables
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PATH="/opt/venv/bin:$PATH" \
|
|
POD_ID=unknown \
|
|
HOME="/app"
|
|
|
|
# Copy virtual environment from builder stage
|
|
COPY --from=builder /opt/venv /opt/venv
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy application code
|
|
COPY . .
|
|
|
|
# Create necessary directories for runtime file creation
|
|
RUN mkdir -p /app/model_manager/reports /app/logs /app/temp /app/models /app/data && \
|
|
chmod 755 /app/model_manager/reports /app/logs /app/temp /app/models /app/data
|
|
|
|
# Create non-root user
|
|
RUN groupadd -r appuser && useradd -r -g appuser appuser && \
|
|
chown -R appuser:appuser /app
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
# Set entrypoint for proper signal handling and PID 1
|
|
ENTRYPOINT ["/opt/venv/bin/python", "-m", "model_manager.worker.worker"]
|