FROM ubuntu:20.04

# 패키지 설치 및 ssh 설치&설정
RUN apt-get update && apt-get -y upgrade && \
    apt-get install -y openssh-server && \
    apt-get install -y mariadb-server && \
    apt-get install -y mariadb-client && \ 
    apt-get install -y vim && \
    apt-get install -y wget && \
    apt-get install -y curl && \
    apt-get install -y iputils-ping && \
    apt-get install -y openjdk-8-jdk && \
    apt-get install -y gnupg2 && \
    apt-get install -y gnupg && \
    apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 사용자 홈 디렉토리 설정
RUN useradd -ms /bin/bash jenkins
RUN chown -R jenkins:jenkins /home/jenkins

# Set up configuration for SSH
RUN mkdir /var/run/sshd && \
    echo 'root:!@#gds$%^' | chpasswd && \
    sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config

# SSH login fix. Otherwise, user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

# Jenkins 설치
RUN curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
RUN echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
RUN apt update && apt upgrade -y
RUN apt-get install -y jenkins

# Jenkins 설정 파일 복사
COPY jenkins_config.xml /var/jenkins_home/config.xml

# 로그 파일 경로 설정
ENV JENKINS_LOG_DIR /var/log/jenkins
RUN mkdir -p $JENKINS_LOG_DIR

# java and tomcat path
RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64" >> /etc/profile && \
    echo "export CATALINA_HOME=/usr/local/tomcat" >> /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

# 포트 노출
EXPOSE 22 8080 50000

# start service
RUN mkdir service
COPY start_service.sh /service
RUN chmod +x /service/start_service.sh
CMD ["/service/start_service.sh"]
