Docker:docker container logsのログファイルの実体を表示する方法

スポンサーリンク

docker container logsのログファイルの実体を表示する方法

docker container logsのログファイルの実体はホストOS上の/var/lib/docker/containers/<コンテナID>/<コンテナID>-json.logとして保存されます。

 

コマンドの説明

$ docker container logs --help

Usage:  docker container logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

 

実行例

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              1d622ef86b13        3 days ago          73.9MB
centos              latest              470671670cac        3 months ago        237MB
centos              7.7.1908            08d05d1d5859        5 months ago        204MB

$ docker container run -it --name test01 centos:7.7.1908 /bin/bash
[root@f09c316aad50 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@f09c316aad50 /]# hostname
f09c316aad50
[root@f09c316aad50 /]# CTRL+P、CTRL+Qを押してコンテナから離脱

$ docker container logs -t test01  # docker container logsでログを表示
2020-04-28T00:15:24.317274574Z [root@f09c316aad50 /]# ls
2020-04-28T00:15:24.367452608Z anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
2020-04-28T00:15:46.870966187Z [root@f09c316aad50 /]# hostname
2020-04-28T00:15:46.892856027Z f09c316aad50

$ ID=$(docker container ls --no-trunc | grep test01 | cut -d" " -f1)  # コンテナIDをシェル変数に代入

$ echo $ID
f09c316aad504848d0b7eab735706e8ada6bdef38f16e2ff3b9e0e850fa5b137

$ sudo ls /var/lib/docker/containers/${ID}
checkpoints  config.v2.json  f09c316aad504848d0b7eab735706e8ada6bdef38f16e2ff3b9e0e850fa5b137-json.log  hostconfig.json  hostname  hosts  mounts  resolv.conf  resolv.conf.hash

$ sudo cat /var/lib/docker/containers/${ID}/${ID}-json.log  # ログファイルの実体を表示
{"log":"\u001b]0;@f09c316aad50:/\u0007\u001b[?1034h[root@f09c316aad50 /]# ls\r\n","stream":"stdout","time":"2020-04-28T00:15:24.317274574Z"}
{"log":"anaconda-post.log  \u001b[0m\u001b[01;36mbin\u001b[0m  \u001b[01;34mdev\u001b[0m  \u001b[01;34metc\u001b[0m  \u001b[01;34mhome\u001b[0m  \u001b[01;36mlib\u001b[0m  \u001b[01;36mlib64\u001b[0m  \u001b[01;34mmedia\u001b[0m  \u001b[01;34mmnt\u001b[0m  \u001b[01;34mopt\u001b[0m  \u001b[01;34mproc\u001b[0m  \u001b[01;34mroot\u001b[0m  \u001b[01;34mrun\u001b[0m  \u001b[01;36msbin\u001b[0m  \u001b[01;34msrv\u001b[0m  \u001b[01;34msys\u001b[0m  \u001b[30;42mtmp\u001b[0m  \u001b[01;34musr\u001b[0m  \u001b[01;34mvar\u001b[0m\r\n","stream":"stdout","time":"2020-04-28T00:15:24.367452608Z"}
{"log":"\u001b]0;@f09c316aad50:/\u0007[root@f09c316aad50 /]# hostname\r\n","stream":"stdout","time":"2020-04-28T00:15:46.870966187Z"}
{"log":"f09c316aad50\r\n","stream":"stdout","time":"2020-04-28T00:15:46.892856027Z"}