# NVIDIA Isaac Lab v2.3.2 on NVIDIA Isaac Sim 5.1.0 - the image we test this package in (Cloud Run, NVIDIA RTX PRO 6000).
# Follows isaac-sim/IsaacLab v2.3.2 docker/Dockerfile.base, plus the fixes this Isaac Sim image needs (commented below).
# Build: docker build -t roaddata-isaaclab:2.3.2 docker/
FROM nvcr.io/nvidia/isaac-sim:5.1.0
USER root
# Cloud Run mounts the NVIDIA graphics driver in /usr/local/nvidia/lib64 but registers no Vulkan ICD: add it (harmless elsewhere)
RUN mkdir -p /etc/vulkan/icd.d /usr/share/vulkan/icd.d && \
    printf '{"file_format_version":"1.0.0","ICD":{"library_path":"libGLX_nvidia.so.0","api_version":"1.4.312"}}' > /etc/vulkan/icd.d/nvidia_icd.json && \
    cp /etc/vulkan/icd.d/nvidia_icd.json /usr/share/vulkan/icd.d/nvidia_icd.json && \
    mkdir -p /usr/share/glvnd/egl_vendor.d && printf '{"file_format_version":"1.0.0","ICD":{"library_path":"libEGL_nvidia.so.0"}}' > /usr/share/glvnd/egl_vendor.d/10_nvidia.json
SHELL ["/bin/bash", "-c"]
ENV LANG=C.UTF-8 DEBIAN_FRONTEND=noninteractive ISAACSIM_ROOT_PATH=/isaac-sim ISAACLAB_PATH=/workspace/isaaclab
RUN apt-get update && apt-get install -y --no-install-recommends build-essential cmake git libglib2.0-0 ncurses-term wget && rm -rf /var/lib/apt/lists/*
RUN git clone --depth 1 --branch v2.3.2 https://github.com/isaac-sim/IsaacLab.git ${ISAACLAB_PATH} && chmod +x ${ISAACLAB_PATH}/isaaclab.sh && ln -sf ${ISAACSIM_ROOT_PATH} ${ISAACLAB_PATH}/_isaac_sim
RUN ${ISAACLAB_PATH}/isaaclab.sh -p -m pip install toml && ${ISAACLAB_PATH}/isaaclab.sh -p ${ISAACLAB_PATH}/tools/install_deps.py apt ${ISAACLAB_PATH}/source && rm -rf /var/lib/apt/lists/*
RUN touch /bin/nvidia-smi /bin/nvidia-debugdump /bin/nvidia-persistenced /bin/nvidia-cuda-mps-control /bin/nvidia-cuda-mps-server /etc/localtime 2>/dev/null; mkdir -p /var/run/nvidia-persistenced && touch /var/run/nvidia-persistenced/socket
# flatdict 4.0.1 (pinned by isaaclab) is sdist-only and imports pkg_resources, which current setuptools dropped: build it first without isolation
RUN ${ISAACLAB_PATH}/isaaclab.sh -p -m pip install --no-deps "setuptools==69.5.1" "wheel==0.43.0" && ${ISAACLAB_PATH}/isaaclab.sh -p -m pip install --no-build-isolation flatdict==4.0.1
# pip/_vendor/packaging in this Isaac Sim image is symlinks into exts/*/pip_prebundle/packaging; uninstalling that packaging 23.0 breaks pip. Make pip self-contained first.
RUN SP=${ISAACSIM_ROOT_PATH}/kit/python/lib/python3.11/site-packages && cp -rL $SP/pip /tmp/pip_real && rm -rf $SP/pip && mv /tmp/pip_real $SP/pip && test -z "$(find $SP/pip -type l)" && ${ISAACLAB_PATH}/isaaclab.sh -p -m pip --version
RUN ${ISAACLAB_PATH}/isaaclab.sh --install rsl_rl && (${ISAACLAB_PATH}/isaaclab.sh -p -m pip uninstall -y quadprog || true)
# isaaclab.sh --install reinstalls torch/torchvision into site-packages; the uninstall leaves empty dirs (no __init__.py) in the ml_archive
# prebundle, which Kit puts first on sys.path -> "torch has no attribute Tensor". Remove those husks, then check torch with the prebundle first.
RUN PB=${ISAACSIM_ROOT_PATH}/exts/omni.isaac.ml_archive/pip_prebundle && SP=${ISAACSIM_ROOT_PATH}/kit/python/lib/python3.11/site-packages && for d in $PB/*/; do n=$(basename $d); if [ ! -e $d/__init__.py ] && [ -e $SP/$n/__init__.py ]; then echo "remove husk $n"; rm -rf $d; fi; done && ${ISAACLAB_PATH}/isaaclab.sh -p -c "import sys; sys.path.insert(0, '$PB'); import torch, torchvision; assert hasattr(torch, 'Tensor'), torch.__path__; print('TORCH OK', torch.__file__, torch.__version__)"
RUN ${ISAACLAB_PATH}/isaaclab.sh -p -c "import importlib.util as u, isaaclab, rsl_rl; missing=[m for m in ('isaaclab_tasks','isaaclab_rl','isaaclab_assets') if u.find_spec(m) is None]; assert not missing, missing; print('isaaclab install OK', isaaclab.__file__)"   # fails the build if the install is incomplete
ENV ACCEPT_EULA=Y PRIVACY_CONSENT=N OMNI_KIT_ACCEPT_EULA=YES NVIDIA_DRIVER_CAPABILITIES=all
WORKDIR /pack
CMD ["bash", "/pack/run/run.sh"]
