FROM pytorch/pytorch:2.1.1-cuda12.1-cudnn8-runtime

ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /sumo

COPY --chown=root ./sumo-system-requirements-linux.txt /sumo
RUN apt-get update \
    && xargs apt-get install -y <./sumo-system-requirements-linux.txt \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN apt-get remove --purge -y libtiff-dev libtiff5

RUN curl -sSL https://sumo.dlr.de/releases/1.16.0/sumo-src-1.16.0.tar.gz | tar -xz \
    && mkdir sumo-1.16.0/build/cmake-build && cd sumo-1.16.0/build/cmake-build \
    && cmake ../.. && make -j$(nproc)

ENV SUMO_HOME=/sumo

WORKDIR /gymnasium

COPY --chown=root ./gymnasium-system-requirements-linux.txt /gymnasium
RUN apt-get update \
    && xargs apt-get install -y <./gymnasium-system-requirements-linux.txt \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /tianshou

RUN curl -sSL https://github.com/thu-ml/tianshou/archive/refs/tags/v1.1.0.tar.gz | tar -xz --strip-components=1
ENV PYTHONPATH="/tianshou"

WORKDIR /app

RUN apt-get update -y \
    && apt-get install -y software-properties-common \
    && xargs -L1 add-apt-repository ppa:deadsnakes/ppa \
    && apt-get install -y python3.11 python3.11-distutils \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

COPY --chown=root ./requirements.txt /app
# "apt-get install -y python3.pip + python3.11 -m pip install --upgrade pip" is throwing errors, so we do an workaround
RUN if ! python3.11 -m pip --version > /dev/null 2>&1; then \
    curl -sSL https://bootstrap.pypa.io/get-pip.py | python3.11; \
    fi \
    && python3.11 -m pip install --no-cache-dir -r requirements.txt \
    && python3.11 -m pip install --no-cache-dir gymnasium[atari]==1.0.0 gymnasium[mujoco]==1.0.0 mo-gymnasium==1.3.1

ENV LOG=/log
ENV RESULTS=/results

COPY --chown=root . /app

ARG USER_ID
ARG GROUP_ID

RUN addgroup --gid $GROUP_ID user \
    && adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user \
    && chown -R $USER_ID:$GROUP_ID /app && chmod -R a=rwx /app

USER user

VOLUME ${LOG}
VOLUME ${RESULTS}

ENTRYPOINT ["python3.11"]
CMD ["src/run.py"]
