# syntax=docker/dockerfile:1
# =============================================================================
# Poly Hammer runner environment image: one image per env id (plan C3).
#
# The image holds a locked dependency set and nothing else: the same
# `uv sync --frozen --no-build --no-install-project` that `ph` runs natively,
# over the catalog's generated build project for a linux-x64 target. Worker
# code (ph_runner.py + poly_hammer_worker) is NOT baked in: `ph` bind-mounts the
# unpacked worker package at /ph/worker, so a worker release never needs a new
# image and never changes an env id.
#
# BUILD_DIR (default: the build context itself) holds the build project:
#   pyproject.toml, uv.lock, wheels/ (PH-built wheels, sha256-checked by the
#   publisher against the catalog before the build; absent when none)
#
# C_COMPILER=1 adds gcc and the C library headers, for envs whose recipes set
# needs.c_compiler (TRELLIS.2: Triton builds a launcher per GPU kernel with
# the host C compiler at first use, against the env Python's headers, which
# the uv-managed Python ships). The catalog's docker plan says which envs
# need it and release-worker-catalog.yml passes it. Default 0: no compiler.
#
# Image tags (the docker plan's `tag`, catalog_common.image_tag) are
# `<env id>-<revision>[-cc]`: the revision is a hash of this file, and `-cc`
# marks a C_COMPILER=1 image. The env id covers neither, and the release never
# rebuilds a tag that exists, so ANY edit here (comments included) re-tags,
# and so rebuilds, every image on the next release, and a compiler image never
# shares a tag with a compiler-free one of the same env.
#
#   docker build -f workers/self-hosted/docker/Dockerfile \
#     --build-arg PYTHON_VERSION=3.12.11 [--build-arg C_COMPILER=1] \
#     -t ph-env:e-0123456789ab-d0123456789ab[-cc] <build directory>
# =============================================================================

# ubuntu:24.04, pinned by index digest; bump deliberately (it changes no env id;
# the new revision re-tags and rebuilds every image).
FROM ubuntu:24.04@sha256:534baea6a22c03a63003dbc8dbe78fe34bc0d7e595d9a9dc9834884ff530eb55

# Must match workers/cli/toolchain.toml ([uv] version and the
# x86_64-unknown-linux-musl sha256), so Docker and native installs resolve
# identically. A test in workers/self-hosted/tests checks both.
ARG UV_VERSION=0.13.4
ARG UV_SHA256=28d4385f9f9d1d1c7654d85b3855e0e2615b2f6cc13136a2edff858bbdc16ff1
# The env's exact interpreter (catalog envs[].python).
ARG PYTHON_VERSION

ENV UV_PYTHON_INSTALL_DIR=/ph/python \
    UV_PROJECT_ENVIRONMENT=/ph/env \
    UV_PYTHON_PREFERENCE=only-managed \
    UV_LINK_MODE=copy \
    UV_COMPILE_BYTECODE=1 \
    UV_NO_CACHE=1 \
    PYTHONUTF8=1 \
    PH_RUNNER=1 \
    HF_HUB_OFFLINE=1 \
    TRANSFORMERS_OFFLINE=1

RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates curl \
    && curl -fsSL -o /tmp/uv.tar.gz \
        "https://github.com/astral-sh/uv/releases/download/${UV_VERSION}/uv-x86_64-unknown-linux-musl.tar.gz" \
    && echo "${UV_SHA256}  /tmp/uv.tar.gz" | sha256sum -c - \
    && tar -x -f /tmp/uv.tar.gz -C /tmp \
    && install -m 0755 /tmp/uv-x86_64-unknown-linux-musl/uv /usr/local/bin/uv \
    && rm -rf /tmp/uv.tar.gz /tmp/uv-x86_64-unknown-linux-musl \
    && apt-get purge -y curl \
    && apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/*

# Only when a recipe compiles kernels on the machine (see above): gcc is what
# Triton tries after $CC, libc6-dev its C library headers.
ARG C_COMPILER=0
RUN case "${C_COMPILER}" in \
      0) ;; \
      1) apt-get update \
         && apt-get install -y --no-install-recommends gcc libc6-dev \
         && rm -rf /var/lib/apt/lists/* ;; \
      *) echo "C_COMPILER must be 0 or 1, not ${C_COMPILER}" >&2; exit 1 ;; \
    esac

# The build directory, relative to the build context (default: the context is
# the build directory itself).
ARG BUILD_DIR=.
COPY ${BUILD_DIR}/ /ph/build/

RUN test -n "${PYTHON_VERSION}" || (echo "PYTHON_VERSION is required" >&2; exit 1) \
    && test -f /ph/build/pyproject.toml && test -f /ph/build/uv.lock \
    && mkdir -p /ph/build/wheels \
    && uv python install "${PYTHON_VERSION}" \
    && uv sync --frozen --no-build --no-install-project \
        --project /ph/build --python "${PYTHON_VERSION}" \
    && /ph/env/bin/python -c "import sys; print(sys.version)" \
    && if [ "${C_COMPILER}" = "1" ]; then \
         include="$(/ph/env/bin/python -c 'import sysconfig; print(sysconfig.get_paths()["include"])')" \
         && printf '#include <Python.h>\nint main(void) { return 0; }\n' \
            | gcc -fsyntax-only -I"${include}" -x c - \
         && echo "C compiler and Python headers ready (${include})"; \
       fi

# /ph/worker: the worker package (bind mount, read-only)
# /ph/models: model files (bind mounts, read-only)
# /ph/job:    job directories (bind mount, read-write)
# /ph/cache:  runtime caches (bind mount of <storage>/cache/runtime,
#             read-write); `ph` points HF_HOME, TORCH_HOME and
#             TRITON_CACHE_DIR at its hf/, torch/ and triton/, so compiled
#             Triton kernels outlive the --rm container
WORKDIR /ph
# The env's Python first on PATH: Modal runs its client with `python` from the
# image (workers/modal), and `docker exec` debugging finds the env's tools.
ENV PATH=/ph/env/bin:$PATH
# -X utf8: -I ignores PYTHONUTF8, so UTF-8 mode must be on the command line.
ENTRYPOINT ["/ph/env/bin/python", "-X", "utf8", "-I", "/ph/worker/ph_runner.py"]
