# ─────────────────────────────────────────────────────────────────────────────
# Dockerfile — TensorFlow GAN Starter
# Builds a CPU-only container for the FastAPI inference service.
#
# Build:
#   docker build -t tensorflow-gan-starter .
#
# Run (API):
#   docker run -p 8000:8000 \
#     -v "$(pwd)/outputs:/app/outputs" \
#     tensorflow-gan-starter
#
# Run (training, no API):
#   docker run -v "$(pwd)/outputs:/app/outputs" \
#     tensorflow-gan-starter \
#     python scripts/run.py --mode train
#
# GPU (Linux only): replace the base image with tensorflow/tensorflow:2.15.0-gpu
# and set CUDA_VISIBLE_DEVICES in the environment.
# ─────────────────────────────────────────────────────────────────────────────

FROM python:3.11-slim AS base

# Prevent Python from writing .pyc files and buffer stdout/stderr
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    TF_CPP_MIN_LOG_LEVEL=2 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

WORKDIR /app

# ── System dependencies ───────────────────────────────────────────────────────
# libgomp1  : required by TensorFlow for OpenMP thread parallelism
# libglib2.0: required by some image libraries
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgomp1 \
    libglib2.0-0 \
    && rm -rf /var/lib/apt/lists/*

# ── Python dependencies ───────────────────────────────────────────────────────
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# ── Application code ──────────────────────────────────────────────────────────
COPY config/   config/
COPY src/      src/
COPY api/      api/
COPY app/      app/
COPY scripts/  scripts/
COPY examples/ examples/
COPY .env.example .env.example

# Create runtime directories (mounted volumes will override these)
RUN mkdir -p outputs/checkpoints outputs/samples outputs/logs data

# ── Non-root user (security best practice) ────────────────────────────────────
RUN useradd --create-home appuser && chown -R appuser /app
USER appuser

# ── Default command: launch FastAPI via Uvicorn ───────────────────────────────
EXPOSE 8000
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
