# CARLA 0.9.16 (UE4) server + Python 3.10 client + ScenarioRunner v0.9.16 - the image we test this package in (Cloud Run, NVIDIA RTX PRO 6000).
# Build: docker build -t roaddata-carla:0.9.16 docker/
FROM carlasim/carla:0.9.16
USER root
# Cloud Run mounts the NVIDIA graphics driver in /usr/local/nvidia/lib64 but registers no Vulkan ICD; 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
RUN apt-get update && apt-get install -y --no-install-recommends python3 python3-pip python3-venv git ffmpeg libvulkan1 xdg-user-dirs && rm -rf /var/lib/apt/lists/* && \
    python3 --version
# ScenarioRunner pinned to its v0.9.16 release (master imports py_trees.decorators, which its own py-trees==0.8.3 pin lacks).
# Ubuntu 20.04 base has Python 3.8 and no bundled client; the PyPI carla 0.9.16 client needs 3.10+.
# uv installs a self-contained CPython 3.10 (python-build-standalone) without touching the system Python.
RUN python3 -m pip install --no-cache-dir uv && \
    uv venv --python 3.10 /opt/venv && \
    uv pip install --python /opt/venv/bin/python --no-cache "carla==0.9.16" numpy pillow shapely networkx py_trees==0.8.3 xmlschema ephem tabulate simple-watchdog-timer six lxml && \
    git clone --depth 1 --branch v0.9.16 https://github.com/carla-simulator/scenario_runner.git /opt/scenario_runner && \
    uv pip install --python /opt/venv/bin/python --no-cache -r /opt/scenario_runner/requirements.txt && \
    git clone --depth 1 --branch 0.9.16 --filter=blob:none --no-checkout https://github.com/carla-simulator/carla.git /opt/carla_src && \
    git -C /opt/carla_src checkout HEAD -- PythonAPI/carla/agents && ls /opt/carla_src/PythonAPI/carla/agents && \
    uv pip install --python /opt/venv/bin/python --no-cache "setuptools<70" && \
    cd /opt/scenario_runner && PYTHONPATH=/opt/scenario_runner:/opt/carla_src/PythonAPI/carla /opt/venv/bin/python -c "import scenario_runner, srunner.tools.openscenario_parser; print('scenario_runner import ok')" && \
    /opt/venv/bin/python -c "import carla, sys; print('carla client ok', sys.version)"
ENV PATH=/opt/venv/bin:$PATH
ENV NVIDIA_DRIVER_CAPABILITIES=all LD_LIBRARY_PATH=/usr/local/nvidia/lib64 SCENARIO_RUNNER_ROOT=/opt/scenario_runner PYTHONPATH=/opt/scenario_runner:/opt/carla_src/PythonAPI/carla
WORKDIR /pack
CMD ["bash", "/pack/run/run_scenario.sh"]
