# NiftyReg v2.1.1 (official KCL-BMEIS Ubuntu release) in Docker.
#
# The host (Ubuntu 20.04, glibc 2.31) is too old for the v2.x binaries, which
# need GLIBC_2.34; the nvidia/cuda ubuntu22.04 base (glibc 2.35) runs them
# directly.  The CUDA runtime provides libcusolver.so.11, which the
# libniftyreg_cuda.so plugin (dlopen'd by reg_aladin with -platf 1) links
# against; libcuda.so.1 comes from the host driver via `docker run --gpus all`.
# Build:
#   docker build -t niftyreg:2.1.1-ubuntu niftyreg/
# Run (GPU, the pipeline's default):
#   docker run --rm --gpus all -v /path/to/work:/work -w /work \
#       niftyreg:2.1.1-ubuntu -ref ref.nii.gz -flo flo.nii.gz \
#       -rigOnly -aff aff.txt -platf 1
# Run (CPU fallback):
#   docker run --rm -v /path/to/work:/work -w /work \
#       niftyreg:2.1.1-ubuntu -ref ref.nii.gz -flo flo.nii.gz \
#       -rigOnly -aff aff.txt -platf 0
# reg_aladin is the ENTRYPOINT, so the NiftyReg CLI flags follow directly.
# -platf 0|1 = CPU|CUDA, -gpuid selects the device, -omp sets OpenMP threads.
FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04

RUN apt-get update && apt-get install -y --no-install-recommends \
        libgomp1 libstdc++6 zlib1g \
    && rm -rf /var/lib/apt/lists/*

COPY NiftyReg-Ubuntu-v2.1.1/ /niftyreg/
# reg_aladin searches /usr/local/lib for the CUDA plugin
RUN chmod +x /niftyreg/* \
    && cp /niftyreg/libniftyreg_cuda.so /usr/local/lib/ \
    && ldconfig

ENV PATH=/niftyreg:$PATH \
    LD_LIBRARY_PATH=/niftyreg:/usr/local/lib

ENTRYPOINT ["reg_aladin"]