Dockerログを確認する方法は?

公開: 2021-08-30

Dockerは、人気のあるOSレベルの仮想化プラットフォームであり、コンテナーと呼ばれるパッケージでアプリケーションを提供できます。これは、簡単に言えば、独自のソフトウェア、ライブラリ、および構成ファイルを備えた分離された環境です。

他の最新のソフトウェアと同様に、警告やエラーなどのイベントやメッセージのログ記録はDockerプラットフォームの固有の部分であり、アプリケーションと本番環境の問題をデバッグできます。

コンテナのログを管理および監視するための簡単な方法について説明します。 それでは始めましょう。

DockerLogsコマンド

コンテナのログをフェッチするための基本的な構文は次のとおりです。

 $ docker logs [OPTIONS] <CONTAINER-NAME OR ID>

また

$ docker container logs [OPTIONS] <CONTAINER-NAME OR ID>

どちらの構文も基本的に同じであるため、この記事では、 docker logsとして残りのコマンドに焦点を当てます。

ただし、上記のコマンドは、 json-fileまたはjournaldログドライバーで開始されたコンテナーに対してのみ機能することに注意してください。

ここで、 OPTIONSは、以下にリストされているdocker logsコマンドで使用可能なサポートされているフラグを参照します。

名前、速記ディフォルト説明
--details ログに提供された追加の詳細を表示します。
--follow-f ログ出力に従ってください
--since タイムスタンプ(例:2021-08-28T15:23:37Z)または相対(例:56分で56分)以降のログを表示する
--tail , -n all ログの最後から表示する行数
--timestamps, -t タイムスタンプを表示する
--until API 1.35+
タイムスタンプ(例:2021-08-28T15:23:37Z)または相対(例:56分で56分)の前にログを表示する

例:

 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 28913415ed22 nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second 80/tcp gifted_edison $ docker logs 28913415ed22 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 09:02:59 [notice] 1#1: using the "epoll" event method 2021/08/28 09:02:59 [notice] 1#1: nginx/1.21.1 2021/08/28 09:02:59 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 09:02:59 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 09:02:59 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 09:02:59 [notice] 1#1: start worker processes 2021/08/28 09:02:59 [notice] 1#1: start worker process 31 2021/08/28 09:02:59 [notice] 1#1: start worker process 32 $

Dockerログの場所

Dockerは、デフォルトで、すべてのコンテナーの標準出力(および標準エラー)をキャプチャし、JSON形式を使用してファイルに書き込みます。 これは、JSONファイルロギングドライバーまたはjson-fileを使用して実現されます。 これらのログは、デフォルトで/var/lib/dockerファイルシステムの下のコンテナ固有の場所に保存されます。

 /var/lib/docker/containers/<container_id>/<container_id>-json.log

例として、以下にリストされている私のredisコンテナーの場合、以下のスニペットに示すように、そのjsonログファイルを確認できます。

 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 551c9273bbea redis "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 6379/tcp redis 6cc871763df1 nginx "/docker-entrypoint.…" 7 hours ago Up 7 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp nostalgic_wescoff $ sudo ls -l /var/lib/docker/containers/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73-json.log -rw-r----- 1 root root 1437 Aug 28 16:53 /var/lib/docker/containers/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73-json.log $ sudo tail -10 /var/lib/docker/containers/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73/551c9273bbea6eaf66523ed735866b9ebe6924c3b504dfeb44bef90e69d59c73-json.log {"log":"1:C 28 Aug 2021 16:53:42.160 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo\n","stream":"stdout","time":"2021-08-28T16:53:42.16031257Z"} {"log":"1:C 28 Aug 2021 16:53:42.160 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started\n","stream":"stdout","time":"2021-08-28T16:53:42.160337871Z"} {"log":"1:C 28 Aug 2021 16:53:42.160 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf\n","stream":"stdout","time":"2021-08-28T16:53:42.160342171Z"} {"log":"1:M 28 Aug 2021 16:53:42.160 * monotonic clock: POSIX clock_gettime\n","stream":"stdout","time":"2021-08-28T16:53:42.160792578Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 * Running mode=standalone, port=6379.\n","stream":"stdout","time":"2021-08-28T16:53:42.161148683Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 # Server initialized\n","stream":"stdout","time":"2021-08-28T16:53:42.161170984Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.\n","stream":"stdout","time":"2021-08-28T16:53:42.161186984Z"} {"log":"1:M 28 Aug 2021 16:53:42.161 * Ready to accept connections\n","stream":"stdout","time":"2021-08-28T16:53:42.161484389Z"} $

追加の詳細を表示

ログに提供される追加の詳細を表示するには、 --detailsフラグを使用し--details

例:

 $ docker logs 6cc871763df1 --details /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

ログ出力に従ってください

--followまたは-fフラグを使用して、ログ出力を追跡できます。 これにより、 STDOUTおよびSTDERRから継続的にログストリーム内の新しい更新を監視できます。

例:

 $ docker logs 6cc871763df1 -f /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-"

テールログ

コンテナログは、 --tailまたは-nフラグを--tailして、画面に表示される出力の数を制限するように--tailできます。 デフォルトでは、このフラグはallを完全なログストリームを示す引数と見なします。 ログの最後から固定数の行を表示するには、 --tailまたは-nフラグの後に正の整数を指定します。

例:

 $ docker logs 6cc871763df1 -n 10 /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

以降のログを表示

我々が使用してログ出力を制限することができる--sinceフラグおよびシンタックスとの絶対値のようなタイムスタンプを与える2021-08-28T15:23:37Z等に対する1 56m 56分間。

--sinceオプションは、特定の日付以降に生成されたコンテナログのみを表示します。 日付は、RFC 3339日付、UNIXタイムスタンプ、またはGo期間文字列( 1m30s3h )として指定できます。 タイムスタンプの最後にZまたは+-00:00タイムゾーンオフセットを指定しない場合、クライアントのローカルタイムゾーンが使用されます。 あなたは組み合わせることができ--sinceいずれかまたは両方のオプションをして--follow--tailオプション。

例:

 $ docker logs --since=1m nostalgic_wescoff 172.17.0.1 - - [28/Aug/2021:15:19:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:25 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

上記の例では、わずか2分以降のログが表示されます。ここで、 nostalgic_wescoffは、 nginxコンテナーに割り当てられた自動生成された名前です。

までログを表示

よう--sinceフラグ、 docker logs 、支持--until示すログ所与のタイムスタンプの前に旗を、。 同様に、タイムスタンプは以前と同様の規則に従い、構文2021-08-28T15:23:37Z絶対値、または56mの56分間の相対値として指定できます。

例:

 $ docker logs --until=1h30m nostalgic_wescoff /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/08/28 10:29:05 [notice] 1#1: using the "epoll" event method 2021/08/28 10:29:05 [notice] 1#1: nginx/1.21.1 2021/08/28 10:29:05 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 2021/08/28 10:29:05 [notice] 1#1: OS: Linux 5.8.0-1039-azure 2021/08/28 10:29:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/08/28 10:29:05 [notice] 1#1: start worker processes 2021/08/28 10:29:05 [notice] 1#1: start worker process 33 2021/08/28 10:29:05 [notice] 1#1: start worker process 34 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

上記の例では、1時間30分前のすべてのログが表示されます。

タイムスタンプを表示する

多くのコンテナアプリケーションは、ログ出力にタイムスタンプが組み込まれているため、Dockerはdocker logsコマンドでタイムスタンプを表示します。 Dockerで出力のタイムスタンプに明示的にプレフィックスを付ける必要がある場合は、 --timestampsまたは-tフラグを使用します。

例:

 $ docker logs -t redis 2021-08-28T16:53:42.160312570Z 1:C 28 Aug 2021 16:53:42.160 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2021-08-28T16:53:42.160337871Z 1:C 28 Aug 2021 16:53:42.160 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started 2021-08-28T16:53:42.160342171Z 1:C 28 Aug 2021 16:53:42.160 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 2021-08-28T16:53:42.160792578Z 1:M 28 Aug 2021 16:53:42.160 * monotonic clock: POSIX clock_gettime 2021-08-28T16:53:42.161148683Z 1:M 28 Aug 2021 16:53:42.161 * Running mode=standalone, port=6379. 2021-08-28T16:53:42.161170984Z 1:M 28 Aug 2021 16:53:42.161 # Server initialized 2021-08-28T16:53:42.161186984Z 1:M 28 Aug 2021 16:53:42.161 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 2021-08-28T16:53:42.161484389Z 1:M 28 Aug 2021 16:53:42.161 * Ready to accept connections $

マージフラグ

Dockerは、すべてのログコンテンツを画面に出力するのではなく、特定のフラグを組み合わせて、よりフィルター処理された出力を取得することを提案します。 簡単な例として、我々は組み合わせることができます--tailでフラグを--sinceより制限された出力を取得します。

例:

 $ docker logs --since=2h -f nostalgic_wescoff 172.17.0.1 - - [28/Aug/2021:15:19:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:25 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-"

これは他のフラグでも機能します。

シェルユーティリティでフィルタリング

Linuxシェルユーティリティを使用して、ログ出力をより巧妙にすることもできます。 grepheadtailなどのユーティリティは、より高度な操作のためにdocker logs出力にパイプすることができます。

例:

 $ docker logs --since=7h nostalgic_wescoff 2>&1 | grep GET 172.17.0.1 - - [28/Aug/2021:10:29:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" 172.17.0.1 - - [28/Aug/2021:15:19:25 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.68.0" "-" $

ここで、 2>&1を使用してgrep単一のパイプ入力を提供するために、ログストリームをリダイレクトする必要があることに注意してください。

概要

Dockerは、環境を管理するための多数の機能を提供する多用途のプラットフォームです。 システムのログの管理は、すべてのシステム管理者が知っておくべき重要なスキルの1つです。 要件に応じて使用可能なコマンドと可能なフラグがわかれば、Dockerでのログの管理は簡単です。

Dockerとその機能の詳細については、Dockerのドキュメントを参照してください。