FROM ubuntu:22.04

# 관련 패키지 설치
RUN apt-get update && apt-get install -y mecab libmecab-dev mecab-ipadic-utf8 vim gcc g++ python3.10 python3-pip locales

RUN localedef -f UTF-8 -i ko_KR ko_KR.UTF-8
ENV LC_ALL ko_KR.UTF-8
ENV PYTHONIOENCODING=utf-8

# jdk 설치
RUN apt-get install -y openjdk-8-jdk

# eunjeon 패키지 따로 설치
RUN pip install eunjeon

# jdk 설정
RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64" >> /etc/profile && \
    echo "export CLASSPATH=.:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/jsp-api.jar:$CATALINA_HOME/lib/servlet-api.jar" >> /etc/profile && \
    echo "export LANG=ko_KR.UTF-8" >> /etc/profile

WORKDIR /fastapi
# 이미지 내부에 필요한 파일을 복사
COPY ./fastapi .

RUN apt-get update && \
    apt-get install -y \
    wget \
    curl \
    gnupg \
    unzip \
    jq \
    libu2f-udev \
    libappindicator3-1 \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libcups2 \
    libdbus-1-3 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libx11-xcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxss1 \
    libxtst6 \
    xdg-utils \
    fonts-liberation \
    libgbm1 \
    libvulkan1 \
    && apt-get clean
    
# Chrome과 Chromedriver 최신 안정 버전 설치
RUN LATEST_STABLE_JSON=$(wget -qO- https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json) && \
    CHROME_URL=$(echo $LATEST_STABLE_JSON | jq -r '.channels.Stable.downloads.chrome[] | select(.platform=="linux64") | .url') && \
    DRIVER_URL=$(echo $LATEST_STABLE_JSON | jq -r '.channels.Stable.downloads.chromedriver[] | select(.platform=="linux64") | .url') && \
    wget -O chrome-linux64.zip $CHROME_URL && \
    wget -O chromedriver-linux64.zip $DRIVER_URL && \
    unzip chrome-linux64.zip && \
    unzip chromedriver-linux64.zip && \
    apt-get update && \
    while read pkg; do apt-get satisfy -y --no-install-recommends "${pkg}"; done < chrome-linux64/deb.deps && \
    mv chrome-linux64/chrome /usr/local/bin/google-chrome && \
    mv chromedriver-linux64/chromedriver /usr/local/bin/chromedriver && \
    chmod +x /usr/local/bin/google-chrome && \
    chmod +x /usr/local/bin/chromedriver && \
    rm chrome-linux64.zip chromedriver-linux64.zip && \
    rm -rf chrome-linux64 chromedriver-linux64

# 크롬 실행 확인
RUN google-chrome --version
RUN chromedriver --version

RUN mkdir -p /dev/shm && chmod 1777 /dev/shm

RUN pip install --no-cache-dir -r requirements.txt

#CMD
CMD ["uvicorn", "app.server:app", "--host", "0.0.0.0", "--port", "8000", "--log-level", "trace"]
