FROM nvidia/cuda:12.4.1-base-ubuntu22.04

ENV DEBIAN_FRONTEND=noninteractive
ENV PATH=/usr/local/nvidia/bin:${PATH}
ENV LD_LIBRARY_PATH=/usr/local/nvidia/lib64:/usr/local/cuda/lib64:${LD_LIBRARY_PATH}

# 필수 패키지 설치
RUN apt-get update && apt-get install -y \
    python3.10 python3.10-dev python3-pip \
    default-jdk curl git build-essential \
    vim htop net-tools libaio-dev \
    wget unzip automake autoconf \
    swig libssl-dev pkg-config && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Python 3.10을 기본 Python으로 설정
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1

# cuDNN 9.3 수동 설치
RUN mkdir -p /usr/local/cuda/include /usr/local/cuda/lib64 && \
    wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-9.3.0.75_cuda12-archive.tar.xz && \
    tar -xvf cudnn-linux-x86_64-9.3.0.75_cuda12-archive.tar.xz && \
    cp -P cudnn-linux-x86_64-9.3.0.75_cuda12-archive/include/* /usr/local/cuda/include/ && \
    cp -P cudnn-linux-x86_64-9.3.0.75_cuda12-archive/lib/libcudnn* /usr/local/cuda/lib64/ && \
    chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn* && \
    rm -rf cudnn-linux-x86_64-9.3.0.75_cuda12-archive*

# MeCab-ko 및 mecab-ko-dic 설치
RUN cd /tmp && \
    wget https://bitbucket.org/eunjeon/mecab-ko/downloads/mecab-0.996-ko-0.9.2.tar.gz && \
    tar xvfz mecab-0.996-ko-0.9.2.tar.gz && \
    cd mecab-0.996-ko-0.9.2 && \
    ./configure --build=x86_64-unknown-linux-gnu && \
    make && make install && ldconfig && \
    cd /tmp && \
    wget https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/mecab-ko-dic-2.1.1-20180720.tar.gz && \
    tar xvfz mecab-ko-dic-2.1.1-20180720.tar.gz && \
    cd mecab-ko-dic-2.1.1-20180720 && \
    ./autogen.sh && ./configure && make && make install && \
    rm -rf /tmp/*

# Java 환경 변수 설정
ENV JAVA_HOME="/usr/lib/jvm/default-java"
ENV PATH="$JAVA_HOME/bin:$PATH"

WORKDIR /workspace

# Python 패키지 설치
COPY requirements.txt ./
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

# TensorFlow libdevice 문제 해결
RUN mkdir -p /usr/local/cuda/nvvm/libdevice && \
    if [ -f /usr/local/lib/python3.10/dist-packages/triton/backends/nvidia/lib/libdevice.10.bc ]; then \
        cp /usr/local/lib/python3.10/dist-packages/triton/backends/nvidia/lib/libdevice.10.bc /usr/local/cuda/nvvm/libdevice/; \
    fi

# TensorFlow 환경변수 설정
ENV CUDA_HOME=/usr/local/cuda
ENV XLA_FLAGS=--xla_gpu_cuda_data_dir=/usr/local/cuda
ENV TF_XLA_FLAGS=--tf_xla_enable_xla_devices=false

# Jupyter 설정
RUN jupyter notebook --generate-config && \
    echo "c.NotebookApp.ip = '0.0.0.0'" >> ~/.jupyter/jupyter_notebook_config.py && \
    echo "c.NotebookApp.allow_root = True" >> ~/.jupyter/jupyter_notebook_config.py && \
    echo "c.NotebookApp.token = ''" >> ~/.jupyter/jupyter_notebook_config.py && \
    echo "c.FileContentsManager.delete_to_trash = False" >> ~/.jupyter/jupyter_notebook_config.py

EXPOSE 8888

CMD ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--allow-root", "--no-browser"]