#syntax=docker/dockerfile:1.7.1@sha256:a57df69d0ea827fb7266491f2813635de6f17269be881f696fbfdf2d83dda33e

# spiced is built against the CUDA 12.6 toolkit (.github/actions/spiced-cuda-build),
# and CUDA 12.x runtime libraries are backward compatible with binaries built on
# an older 12.x toolkit, so any 12.x runtime >= 12.6 works. Keep this on the
# newest 12.x tag NVIDIA still refreshes, and keep both stages on the same image.

FROM nvidia/cuda:12.9.2-cudnn-runtime-ubuntu24.04@sha256:070f8f2672df1b05b84c0409a5fd1d54ddfd646e5b9d8dee7878131271b563fc AS binary

ARG REL_VERSION
ARG CUDA_COMPUTE_CAP=80
ARG GITHUB_REPOSITORY=spiceai/spiceai
# sha256 of the downloaded release asset. When set, the build fails on any
# other file. spiced_docker.yml passes the sha256 publish_release recorded.
ARG SPICED_SHA256=

RUN if [ -z "$REL_VERSION" ]; then echo "REL_VERSION build-arg is required" >&2; exit 1; fi

RUN apt-get update \
    && apt-get install --yes --no-install-recommends ca-certificates curl tar \
    && rm -rf /var/lib/{apt,dpkg,cache,log}

ENV REL_VERSION=${REL_VERSION}
ENV CUDA_COMPUTE_CAP=${CUDA_COMPUTE_CAP}

RUN set -eux; \
    asset="spiced_cuda_${CUDA_COMPUTE_CAP}_linux_x86_64.tar.gz"; \
    base_url="https://github.com/${GITHUB_REPOSITORY}/releases/download/v${REL_VERSION}/${asset}"; \
    curl -fSLo /tmp/spiced.tar.gz "${base_url}"; \
    if [ -n "${SPICED_SHA256}" ]; then echo "${SPICED_SHA256}  /tmp/spiced.tar.gz" | sha256sum -c -; fi; \
    tar -C /root -xzf /tmp/spiced.tar.gz; \
    chmod +x /root/spiced; \
    rm -f /tmp/spiced.tar.gz

# PDFium is dlopen'd at runtime by the document parser (not linked into spiced),
# so ship the shared library next to the binary. The release tag must match
# PDFIUM_RELEASE_TAG in liteparse-pdfium-sys (chromium/7897). CUDA builds are
# x86_64 only.
RUN set -eux; \
    curl -fSLo /tmp/pdfium.tgz "https://github.com/run-llama/pdfium-binaries/releases/download/chromium%2F7897/pdfium-linux-x64.tgz"; \
    echo "af96f21fd8e9d53955013dad1d17b003d0120025e787b7ce611a685d57287594  /tmp/pdfium.tgz" | sha256sum -c -; \
    mkdir -p /tmp/pdfium; \
    tar -C /tmp/pdfium -xzf /tmp/pdfium.tgz; \
    cp /tmp/pdfium/lib/libpdfium.so /root/libpdfium.so; \
    rm -rf /tmp/pdfium /tmp/pdfium.tgz

# `duckdb:` and `ducklake:` datasets read through the DuckDB library installed
# on the host, loaded by name at runtime. Keep DUCKDB_VERSION at the newest
# release test/duckdb_versions.json lists.
ARG DUCKDB_VERSION=2.0.0
ARG DUCKDB_SHA256_AMD64=993ad89e7dfca66cd6da994c2cad090c2fc160ddf9f550498a098ab4bb13cfb1
RUN set -eux; \
    curl -fSLo /tmp/libduckdb.tar.gz "https://github.com/duckdb/duckdb/releases/download/v${DUCKDB_VERSION}/duckdb-shared-libs-linux-amd64.tar.gz"; \
    echo "${DUCKDB_SHA256_AMD64}  /tmp/libduckdb.tar.gz" | sha256sum -c -; \
    tar -C /root -xzf /tmp/libduckdb.tar.gz libduckdb.so; \
    rm -f /tmp/libduckdb.tar.gz

FROM nvidia/cuda:12.9.2-cudnn-runtime-ubuntu24.04@sha256:070f8f2672df1b05b84c0409a5fd1d54ddfd646e5b9d8dee7878131271b563fc AS runtime

ENV CUDA_NVCC_FLAGS=-fPIE

ARG CARGO_FEATURES=default
ARG CUDA_COMPUTE_CAP=80
# Set to a unique value per release build so the apt layer is never served
# from the build cache and picks up the current Ubuntu security updates.
ARG APT_CACHE_BUST=

# Allow DuckDB to load extensions
RUN mkdir /.duckdb/ && chmod 777 /.duckdb/

# Upgrade the base image's packages too: the CUDA base tag is not rebuilt for
# every Ubuntu security update. Only the package lists are removed afterwards
# so the dpkg database stays intact for image scanners. cuda-compat follows the
# base image's CUDA_VERSION (12.9.2 -> cuda-compat-12-9), so a new base tag
# cannot pair with the previous minor's compat package.
RUN echo "apt cache bust: ${APT_CACHE_BUST}" \
    && : "${CUDA_VERSION:?the CUDA base image sets CUDA_VERSION}" \
    && cuda_compat_version="$(echo "${CUDA_VERSION}" | cut -d. -f1,2 | tr . -)" \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get upgrade --yes --no-install-recommends \
    && DEBIAN_FRONTEND=noninteractive apt-get install --yes --no-install-recommends ca-certificates libssl3 tzdata "cuda-compat-${cuda_compat_version}" \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/* /var/log/apt/* /var/log/dpkg.log

RUN if echo "$CARGO_FEATURES" | grep -q "odbc"; then \
    apt-get update \
    && apt-get install --yes --no-install-recommends unixodbc \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/* /var/log/apt/* /var/log/dpkg.log; \
  fi

COPY --from=binary /root/spiced /usr/local/bin/spiced

# Copy PDFium next to the binary (dlopen'd at runtime for PDF document parsing)
COPY --from=binary /root/libpdfium.so /usr/local/bin/libpdfium.so

# Copy libduckdb onto the default library path (loaded by name at runtime for
# `duckdb:` and `ducklake:`)
COPY --from=binary /root/libduckdb.so /usr/lib/libduckdb.so

ENV CUDA_COMPUTE_CAP=${CUDA_COMPUTE_CAP}
ENV LD_LIBRARY_PATH=/usr/local/cuda/compat:${LD_LIBRARY_PATH}

WORKDIR /app

EXPOSE 8090
EXPOSE 9090
EXPOSE 50051

ENTRYPOINT ["/usr/local/bin/spiced"]
CMD ["--http","0.0.0.0:8090","--metrics","0.0.0.0:9090","--flight","0.0.0.0:50051"]