클라우드 + DevOps/Docker

Docker 마운트 실습 :: 마운트를 이용한 NginX 로그 기록 관리

gamjadori 2024. 2. 28. 15:30
728x90

<마운트 실습 :: 마운트를 이용한 NginX 로그 기록 관리>

로그 기록 업데이트 파일 마운트

  • 로그 기록을 마운트 해 자동적으로 업데이트 > 로그 기록 관리
  • 로그 기록 파일 저장 위치: /Labs/ch06/nginx-log

 

1. 로그 기록 저장 폴더 생성: nginx-log

ubuntu@host1:~/Labs/ch06$ mkdir nginx-log
ubuntu@host1:~/Labs/ch06$ ls
bind01  bind02  **nginx-log**

 

2. 로그 기록을 가져올 컨테이너 생성

  • 마운트
    • 호스트: ${PWD}/nginx-log
    • 컨테이너: /var/log/nginx
  • 포트 포워딩 8001(호스트):80(컨테이너)
ubuntu@host1:~/Labs/ch06$ docker container run -d --name=myweb -v ${PWD}/nginx-log:/var/log/nginx -p 8001:80 nginx:1.25.0-alpine
8def3765c57b8e0c16a06dac3119b63ce16c70657c56f724f1e01d03e010aec4
>> nginx 웹 서버가 8001 포트에서 실행되며, 로그는 호스트의 ${PWD}/nginx-log 디렉터리에 저장
ubuntu@host1:~/Labs/ch06$ docker container ps
CONTAINER ID   IMAGE                             COMMAND                   CREATED          STATUS                 PORTS                                       NAMES
8def3765c57b   nginx:1.25.0-alpine               "/docker-entrypoint.…"   8 seconds ago    Up 7 seconds           0.0.0.0:8001->80/tcp, :::8001->80/tcp       myweb
0184f485b0e5   mysql:8.0.35-debian               "docker-entrypoint.s…"   12 minutes ago   Up 12 minutes          3306/tcp, 33060/tcp                         mydb
69db6bdc9620   nginx:1.25.0-alpine               "/docker-entrypoint.…"   3 hours ago      Up 3 hours             80/tcp                                      mginx_mem_1g
fa7bcf3a232b   nginx                             "/docker-entrypoint.…"   3 hours ago      Up 3 hours             80/tcp                                      nginx_mem_1g
20fa045c6714   gcr.io/cadvisor/cadvisor:latest   "/usr/bin/cadvisor -…"   21 hours ago     Up 3 hours (healthy)   0.0.0.0:9559->8080/tcp, :::9559->8080/tcp   cadvisor

 

<엔진엑스 로그 파일 목록 확인>

ubuntu@host1:~/Labs/ch06$ ls -l nginx-log/
합계 8
-rw-r--r-- 1 root root 423  1월 17 13:34 access.log
-rw-r--r-- 1 root root 901  1월 17 13:34 error.log

 

3. 접속 로그 확인

<error.log 로그 확인>

ubuntu@host1:~/Labs/ch06$ tail nginx-log/error.log
2024/01/17 03:40:55 [notice] 1#1: nginx/1.25.0
2024/01/17 03:40:55 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4) 
2024/01/17 03:40:55 [notice] 1#1: OS: Linux 6.5.0-14-generic
2024/01/17 03:40:55 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/01/17 03:40:55 [notice] 1#1: start worker processes
2024/01/17 03:40:55 [notice] 1#1: start worker process 30
2024/01/17 03:40:55 [notice] 1#1: start worker process 31
2024/01/17 03:40:55 [notice] 1#1: start worker process 32
2024/01/17 03:40:55 [notice] 1#1: start worker process 33
2024/01/17 03:40:55 [notice] 1#1: start worker process 34

 

<access.log 로그 확인>

ubuntu@host1:~/Labs/ch06$ tail nginx-log/access.log
192.168.56.1 - - [17/Jan/2024:04:34:54 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-"
192.168.56.1 - - [17/Jan/2024:04:34:54 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "<http://192.168.56.101:8001/>" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-"

 

4. 192.168.56.102로 컨테이너 접속 (접속 로그를 남기기 위해)

ubuntu@host2:~$ curl 192.168.56.101:8001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

5. 접속 로그 기록 확인

ubuntu@host1:~/Labs/ch06$ tail -f nginx-log/access.log
192.168.56.1 - - [17/Jan/2024:04:34:54 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-"
192.168.56.1 - - [17/Jan/2024:04:34:54 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "<http://192.168.56.101:8001/>" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "-"
192.168.56.102 - - [17/Jan/2024:04:49:25 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:07 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:08 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:08 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:08 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:08 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:09 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"
192.168.56.102 - - [17/Jan/2024:04:50:09 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.81.0" "-"