# βββββββββββββββββββββββββββββββββββββββ | |
# π 1οΈβ£ Multi-Stage Build for Smallest Image | |
# βββββββββββββββββββββββββββββββββββββββ | |
FROM python:3.9-slim AS builder | |
# Set a fixed working directory | |
WORKDIR /app | |
# Install essential build tools | |
RUN apt-get update && apt-get install -y gcc g++ python3-dev libffi-dev && \ | |
python3 -m venv venv && \ | |
. venv/bin/activate && \ | |
pip install --no-cache-dir --upgrade pip setuptools wheel | |
# Copy only requirements first for better caching | |
COPY requirements.txt . | |
RUN apt-get install -y gfortran libopenblas-dev && \ | |
. venv/bin/activate && \ | |
pip install --no-cache-dir -r requirements.txt | |
# Copy the rest of the app files | |
COPY . . | |
# βββββββββββββββββββββββββββββββββββββββ | |
# π 2οΈβ£ Runtime Image - Minimal & Secure | |
# βββββββββββββββββββββββββββββββββββββββ | |
FROM python:3.9-slim | |
# Set working directory | |
WORKDIR /app | |
# Copy virtual environment from builder stage | |
COPY --from=builder /app/venv /app/venv | |
# Environment Variables for Performance & Security | |
ENV PATH="/app/venv/bin:$PATH" \ | |
PYTHONUNBUFFERED=1 \ | |
PYTHONDONTWRITEBYTECODE=1 \ | |
APP_USER=appuser | |
# Security: Create and use non-root user | |
RUN addgroup appgroup && adduser appuser && \ | |
adduser appuser appgroup && \ | |
chown -R appuser:appgroup /app | |
USER $APP_USER | |
# Copy application code (Ensures proper permissions) | |
COPY --chown=$APP_USER:appgroup . . | |
# Debugging commands to check working directory and list files | |
RUN pwd && ls -l /app | |
# Use a health check to ensure the container is running correctly | |
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s \ | |
CMD python3 -c 'import os; exit(0 if os.path.exists("summary.py") else 1)' | |
# Use ENTRYPOINT instead of CMD for better runtime control | |
ENTRYPOINT ["python3", "summary.py"] | |