Docker:docker container logsで時刻(タイムスタンプ)付きでコンテナのログを表示する方法

スポンサーリンク

docker container logsで時刻(タイムスタンプ)付きでコンテナのログを表示する方法

時刻(タイムスタンプ)付きでコンテナのログを表示するに-tオプションを指定してdocker container logsコマンドを実行します。

 

コマンドの説明

$ 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
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