Nginxコンテナのアクセスログを確認する方法

  • 投稿者:
  • 投稿カテゴリー:docker

Dockerの標準機能を利用する

docker logsコマンドで確認する

# docker run -d -p 80:80 nginx
# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
f0f70f41d5c8   nginx     "/docker-entrypoint.…"   16 seconds ago   Up 14 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   optimistic_noether

# docker logs -f f0f70f41d5c8
x.x.x.x - - [16/May/2021:12:17:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" "-"

logファイルを確認する

# docker inspect f0f70f41d5c8 | grep LogPath
        "LogPath": "/var/lib/docker/containers/f0f70f41d5c8b7e05b06c7381f26200d8a887c33365dae4eb436abeca55bc537/f0f70f41d5c8b7e05b06c7381f26200d8a887c33365dae4eb436abeca55bc537-json.log",
# tail -f /var/lib/docker/containers/f0f70f41d5c8b7e05b06c7381f26200d8a887c33365dae4eb436abeca55bc537/f0f70f41d5c8b7e05b06c7381f26200d8a887c33365dae4eb436abeca55bc537-json.log
{"log":"x.x.x.x - - [16/May/2021:12:17:48 +0000] \"GET / HTTP/1.1\" 200 612 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36\" \"-\"\n","stream":"stdout","time":"2021-05-16T12:17:48.794003728Z"}

fluentdコンテナを利用する

dockerコンテナのログをfluentdに集める方法を参考に確認した。

# docker run -d -p 127.0.0.1:24224:24224 -p 127.0.0.1:24224:24224/udp \
 -v ${PWD}/log:/fluentd/log \
 -v ${PWD}/fluent.conf:/fluentd/etc/fluent.conf:ro \
 fluent/fluentd:latest \
 /usr/bin/fluentd -c /fluentd/etc/fluent.conf -v

# docker run -d --log-driver=fluentd --log-opt fluentd-address=localhost:24224 --log-opt tag=docker.{{.Name}} -p 80:80 nginx

# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED          STATUS          PORTS                                                              NAMES
94965971f460   nginx                   "/docker-entrypoint.…"   4 seconds ago    Up 2 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp                                  epic_spence
c1e84d37a2f1   fluent/fluentd:latest   "/bin/entrypoint.sh …"   35 seconds ago   Up 33 seconds   5140/tcp, 127.0.0.1:24224->24224/tcp, 127.0.0.1:24224->24224/udp   loving_gauss

# ls log/
docker.b5c2721ec0c3ccf9094ceb9d55c70a8d9.log  docker.b5c2721ec0c3ccf9094ceb9d55c70a8d9.log.meta  docker.log

# tail -f log/docker.b5c2721ec0c3ccf9094ceb9d55c70a8d9.log
2021-05-16T13:06:39+00:00       docker.epic_spence      {"container_id":"94965971f460cac44268ed0cb8a14aa1a430c88feb6f2411145a78e578f3fbbf","container_name":"/epic_spence","source":"stdout","log":"x.x.x.x - - [16/May/2021:13:06:39 +0000] \"GET / HTTP/1.1\" 200 612 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36\" \"-\""}