개요
Inception에서 Docker(Nginx)를 먼저 간!단!히! 살펴보겠습니다.
다른 서비스도 완전히 구성된 상태에서 실행을 보인다는 것을 알아두시길 바랍니다.
디렉토리 구조
.:
total 8
-rw-r--r-- 1 jaehyji jaehyji 0 Aug 27 04:23 dir
-rw-r--r-- 1 jaehyji jaehyji 646 Aug 23 16:38 Makefile
drwxr-xr-x 3 jaehyji jaehyji 4096 Aug 23 16:42 srcs
./srcs:
total 12
-rw-r--r-- 1 jaehyji jaehyji 1154 Aug 23 16:38 docker-compose.yml
-rw-r--r-- 1 jaehyji jaehyji 523 Aug 23 16:53 .env
drwxr-xr-x 5 jaehyji jaehyji 4096 Aug 23 16:37 requirements
./srcs/requirements:
total 12
drwxr-xr-x 4 jaehyji jaehyji 4096 Aug 23 16:37 mariadb
drwxr-xr-x 4 jaehyji jaehyji 4096 Aug 23 16:38 nginx
drwxr-xr-x 4 jaehyji jaehyji 4096 Aug 23 16:37 wordpress
./srcs/requirements/mariadb:
total 12
drwxr-xr-x 2 jaehyji jaehyji 4096 Aug 23 16:37 conf
-rw-r--r-- 1 jaehyji jaehyji 264 Aug 23 16:37 Dockerfile
drwxr-xr-x 2 jaehyji jaehyji 4096 Aug 23 16:37 tools
./srcs/requirements/mariadb/conf:
total 4
-rw-r--r-- 1 jaehyji jaehyji 533 Aug 23 16:37 50-server.cnf
./srcs/requirements/mariadb/tools:
total 4
-rwxr-xr-x 1 jaehyji jaehyji 431 Aug 23 16:37 setup.sh
./srcs/requirements/nginx:
total 12
drwxr-xr-x 2 jaehyji jaehyji 4096 Aug 23 16:37 conf
-rw-r--r-- 1 jaehyji jaehyji 322 Aug 27 04:05 Dockerfile
drwxr-xr-x 2 jaehyji jaehyji 4096 Aug 27 03:51 tools
./srcs/requirements/nginx/conf:
total 4
-rw-r--r-- 1 jaehyji jaehyji 1109 Aug 23 16:37 nginx.conf
./srcs/requirements/nginx/tools:
total 4
-rw-r--r-- 1 jaehyji jaehyji 351 Aug 27 04:05 api_setup.sh
./srcs/requirements/wordpress:
total 12
drwxr-xr-x 2 jaehyji jaehyji 4096 Aug 23 16:37 conf
-rw-r--r-- 1 jaehyji jaehyji 713 Aug 23 16:37 Dockerfile
drwxr-xr-x 2 jaehyji jaehyji 4096 Aug 23 16:37 tools
./srcs/requirements/wordpress/conf:
total 8
-rw-r--r-- 1 jaehyji jaehyji 2768 Aug 23 16:37 wp-config.php
-rw-r--r-- 1 jaehyji jaehyji 222 Aug 23 16:37 www.conf
./srcs/requirements/wordpress/tools:
total 4
-rw-r--r-- 1 jaehyji jaehyji 430 Aug 23 16:37 setup.sh
Nginx(Dockerfile)
FROM debian:11
EXPOSE 443
RUN apt-get update && apt-get install -y nginx tini openssl
COPY ./conf/nginx.conf /etc/nginx/
COPY ./tools/api_setup.sh /bin/
RUN chmod +x /bin/api_setup.sh
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/bin/api_setup.sh"]
각 서비스는 독립적으로 구성되어야 하므로 docker의 nginx 컨테이너가 되겠습니다. 단, 이미 만들어진 이미지 파일을 사용할 수 없습니다. 컨테이너의 기반을 debian으로 설정해두고 사용하도록 합니다. (pid 1번에 대해서는 Inception 마지막 글에서 확인하실 수 있습니다)
https로 웹 사이트를 구성해야 하므로 인증을 위한 openssl을 컨테이너에 설치하고, COPY 명령어를 통해 api_setup과 conf파일을 가상머신(로컬)에서 컨테이너 내부로 옮겨놓고 실행합니다.
Nginx(api_setup.sh)
#!/bin/bash
mkdir -p /etc/nginx/certs/
openssl req -newkey rsa:4096 -x509 -sha256 -nodes \
-out $API_CRT \
-keyout $API_KEY \
-subj "$API_INFO"
chmod 600 $API_KEY
chmod 644 $API_CRT
nginx -g "daemon off;"
api_setup에서는 인증키를 발급하고 키와 인증서에 권한을 부여한 뒤, nginx를 deamon off(포 그라운드)로 실행하도록 합니다. 앞서 언급했듯이, ENTRYPOINT, tini과 같은 pid(1)관련 내용은 Inception 마지막 글에서 따로 서술하겠습니다.
Nginx(nginx.conf)
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
server {
listen 443 ssl default;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
server_name user.42.fr;
ssl_certificate /etc/nginx/certs/user.42.fr.crt;
ssl_certificate_key /etc/nginx/certs/user.42.fr.key;
root /var/www/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_pass wordpress:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
nginx.conf 파일에서는 인증서와 인증키의 경로 그리고 TLSv에 주의하여 구성하시길 바랍니다.
Deamon off
Nginx는 서버(서비스)입니다. 따라서 사용자와 입력을 주고받는 포 그라운드가 아닌 백 그라운드에서 실행이 되는데요. 간단히 쉘에서 포, 백 그라운드에서 프로그램을 실행해보고 이해합시다.
cat
cat은 블로킹 상태로 입력을 대기 받습니다. command는 포 그라운드에서 일반적으로 실행되는데, cat은 사용자의 입력을 받고 출력을 합니다. 그렇다면 cat을 백 그라운드에서 실행하면 어떻게 될까요?
cat 명령어가 중단됐습니다. cat은 프로세스로는 살아있지만 백 그라운드에서 실행됐기때문에 입력을 받을 수 없는 상태로 부모 프로세스(쉘)에 묶여있는 상태가 돼버렸습니다. 백 그라운드에서 입력을 주려면 파이프나 파일 리디렉션 같은 특별한 방법을 사용해야 하겠습니다.
cat 프로세스를 종료하려면 kill 명령어를 사용하거나 부모 프로세스 쉘을 종료하면 됩니다. docker의 nginx도 이런 방식을 따릅니다.
docker는 기본적으로 포 그라운드에 프로그램이 없으면 자신이 실행할 프로그램이 없다고 판단하고 컨테이너를 종료해버립니다. 그래서 nginx를 강제로 포 그라운드로 실행시켜 docker가 종료되지 않도록 만들어야하는 것입니다.
'42Seoul' 카테고리의 다른 글
Inception - Docker(Wordpress) (1) | 2024.08.27 |
---|---|
Inception - Docker(mariaDB) (0) | 2024.08.27 |
Inception - WordPress, MariaDB (0) | 2024.08.24 |
Inception - Nginx (1) | 2024.07.12 |
Inception - Introduction (가상머신 환경) (0) | 2024.02.21 |
댓글