# ── Keras Classification Starter — Dockerfile ─────────────────────────────
# Multi-stage build: keeps the final image lean by not including build tools.
#
# Build:
#   docker build -t keras-classification-starter .
#
# Train (mounts a host directory so the model persists):
#   docker run --rm -v $(pwd)/models:/app/models keras-classification-starter train
#
# Serve the REST API:
#   docker run --rm -p 8000:8000 -v $(pwd)/models:/app/models \
#       keras-classification-starter serve
# ──────────────────────────────────────────────────────────────────────────

# ── Stage 1: base with pinned Python ──────────────────────────────────────
FROM python:3.11-slim AS base

# Disable .pyc files and enable unbuffered stdout/stderr for clean logs.
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

WORKDIR /app

# ── Stage 2: dependency installation ──────────────────────────────────────
FROM base AS deps

COPY requirements.txt .
# TensorFlow's CPU wheel is large; install it first so Docker layer-caches it.
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

# ── Stage 3: final image ───────────────────────────────────────────────────
FROM deps AS final

# Copy project source — omit data/, logs/, models/ (use volumes instead).
COPY config/       ./config/
COPY src/          ./src/
COPY api/          ./api/
COPY app/          ./app/
COPY scripts/      ./scripts/
COPY examples/     ./examples/

# Create directories that may be mounted as volumes at runtime.
RUN mkdir -p models logs data

# Expose FastAPI port.
EXPOSE 8000

# Default entrypoint delegates to the CLI.
# Override CMD at runtime to change behaviour:
#   docker run ... keras-classification-starter train
#   docker run ... keras-classification-starter serve
ENTRYPOINT ["python", "scripts/run.py"]
CMD ["serve", "--host", "0.0.0.0", "--port", "8000"]
