# os with ssh
# images
FROM ubuntu:22.04 

ENV DEBIAN_FRONTEND=noninteractive

# package procedure(update&upgrade&install)
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y vim net-tools zip unzip apt-utils iputils-ping wget tar curl language-pack-ko openssh-server git telnet nmap && \
    apt-get install -y mysql-server mysql-client nginx sudo

# install php7.2
RUN apt-get install -y software-properties-common && \
    add-apt-repository ppa:ondrej/php && \
    apt-get update -y && \
    apt-get install -y php7.2 php7.2-fpm libapache2-mod-php7.2 php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl

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

# Add 'gds' user and set password
RUN useradd -m -s /bin/bash gds
RUN echo 'gds:gds12!@' | chpasswd

# Add 'gds' user to sudoers (Remove NOPASSWD to require password for sudo)
RUN usermod -aG sudo gds

# Disable 'su' command for 'gds' user by restricting access to 'root'
RUN dpkg-reconfigure -plow libpam-runtime
RUN echo "gds ALL=(ALL) ALL" >> /etc/sudoers  # Allow sudo, with password
RUN chmod 750 /bin/su  # Restrict access to 'su' for non-root users

# Set SSH to allow gds login and permit root login
RUN echo "AllowUsers gds" >> /etc/ssh/sshd_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

# Install Node.js (16.20.0) and NPM (8.19.4)
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm@8.19.4

# MySQL 8 install and setting
RUN sed -ri 's/^#?bind-address\s+.*/bind-address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf
RUN sed -ri 's/^#?\s*port\s*=\s*[0-9]+/port = 53306/' /etc/mysql/mysql.conf.d/mysqld.cnf
RUN sed -ri 's/^#?max_allowed_packet\s+.*/max_allowed_packet = 16M/' /etc/mysql/mysql.conf.d/mysqld.cnf
RUN echo "default-time-zone='+09:00'" >> /etc/mysql/mysql.conf.d/mysqld.cnf
RUN echo "character-set-server=utf8mb4" >> /etc/mysql/mysql.conf.d/mysqld.cnf

#run script folder and copy script
RUN mkdir /service_script
COPY start_service.sh /service_script
COPY nodam.sql /service_script
RUN chmod +x /service_script/start_service.sh

#package clean unused
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Expose port(SSH,NGINX,NODEJS,MYSQL)
EXPOSE 22 52530 52532 52533 53306

# Run SSH,NGINX,NODEJS,MYSQL
CMD ["/service_script/start_service.sh"]
