반응형

docker를 사용하여 nginx를 설정하고 실행하는 법을 알아보겠습니다.

 

0. Docker 설치하기

 

1. nginX Image pull

docker pull nginx

 

2. 테스트용 index.html 만들기

<html>
<body>
Hello NginX with Docker!
</body>
</html>

 

3. docker로 nginX 실행하기

docker run --name hello-nginx -v D:\Docker\nginx\html:/usr/share/nginx/html -d -p 80:80 nginx

※ 명령어 설명

# nginx 이미지에 option을 주면서 실행
run nginx 

# 도커 컨테이너 이름을 설정
--name 

# local에 있는 파일or폴더를 nginx 도커의 컨테이너 안의 폴더와 mount
-v

# 백그라운드에서 실행
-d

# 포트 설정 (호스트 포트:도커 컨테이너 포트)
-p

 

4. Docker 컨테이너 실행 확인

>docker run --name hello-nginx -v D:\Docker\nginx\html:/usr/share/nginx/html -d -p 80:80 nginx
f296b29da0f2c9daeb5cd19a608474ed631a70a717a5d04fb1abd1143d121e62

>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
f296b29da0f2        nginx               "/docker-entrypoint.…"   18 seconds ago      Up 17 seconds       0.0.0.0:80->80/tcp   hello-nginx

 

5. localhost 접속 후 확인

[옵션]

nginX conf 설정을 변경하여 반영하고 싶을 경우.

 

1. 설정 파일 생성 (nginx.conf)

생성한 경로

nginx/conf/nginx.conf 

# nginx.conf 파일
# 컨테이너 내부에 /etc/nginx/conf.d/nginx.conf 경로에 존재

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;

    #security
    server_tokens off;
}

 

2. Dockerfile 생성

생성한 경로

nginx/Dockerfile

#Dockerfile
FROM nginx:latest

COPY config/nginx.conf /etc/nginx/conf.d/nginx.conf

CMD ["nginx", "-g", "daemon off;"]

EXPOSE 80

 

3. Docker 이미지를 생성

Dockerfile이 있는 경로에서 실행할 것.

docker build --tag nginx-test:1.0 .

 

4. 생성한 Docker 이미지 확인 후 실행

>docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
nginx-test                1.0                 6e43841d9356        4 weeks ago         133MB
>docker run nginx-test:1.0

 

반응형

+ Recent posts