Tag Archives: 도커 nginx

[Setting | Docker] NGINX 설치

도커에 환경 설정 파일을 변경하여 NGINX를 설치(Dockerizing)하는 방법을 정리한다.


작성일 : 2022-05-27

1> Dockerizing

Docker 컨테이너를 사용하여 어플리케이션을 패킹 & 배포하는 과정을 말함
아래와 같이 설정파일 (Dockerfile)과 리소스를 빌드하여 이미지를 배포함


2> conf.d폴더에 default.conf 파일 생성

server {
    listen       80;
    server_name  van.com;
    location / {
        root   /mnt/c/_Work/source/home;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

3> (변경 필요시) nginx.conf 파일 생성

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

4> Dockerfile 생성

# nginx최신 stable 버전 컨테이너를 커스텀
FROM nginx:1.22.0

# 기존 설정파일 삭제
RUN rm /etc/nginx/conf.d/default.conf
# 수정한 설정파일을 복사
COPY conf.d/default.conf /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

# nginx 기동
CMD ["nginx", "-g", "daemon off;"]

# 포트는 80
EXPOSE 80

5> 컨테이너 생성 및 실행

Dockerfile이 위치한 같은 폴더에서 실행해야함

# 이미지 빌드
sudo docker build --tag nginx-opendocs:1.0 .

# 네트워크 확인 & opdnet IP 대역확인
sudo docker network ls
# - 없을 경우 생성
sudo docker network create --gateway 172.20.0.1 --subnet 172.20.0.0/21 opdnet
# - 네트워크 확인
sudo docker inspect opdnet
# ------------------------------------------
# IPAM > Config > Gateway와 같은대역 IP 지정해야함
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
# Containers에 사용중인 IP는 중복되지 않아야함
        "Containers": {
            "d4c422b0ceb65ab5f45e2efe2c7da3137b7d66a985f3f222b1b974763529ea7c": {
                "Name": "sample",
                "EndpointID": "e0dc778cf7fe452b45c5bec1737050395c553d764de75f3a88aef565fe00166d",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
# ------------------------------------------

# 컨테이너 설치 및 실행 (고정IP지정)
sudo docker run \
--name nginx-opendocs \
--network opdnet --ip 172.20.0.100 \
-d -p 80:80 \
-it \
-v /mnt:/mnt \
nginx-opendocs:1.0

6> 컨테이너 쉘에 접속하여 확인

컨테이너와 OS간 공유볼륨(5>에서 생성된 볼륨 > /mnt) 확인도 필요함

sudo docker run --rm -it nginx-opendocs:1.0 /bin/bash