본문 바로가기
CS/Docker

docker log 확인하는 방법 / docker logs 명령에서 tail, since 사용하는 방법

by Warehaus 2022. 4. 15.

 

Docker log

 

도커 명령어를 이용하여 생성 된 컨테이너의 log를 확인할 수 있습니다.

 

우선 실행 중인 컨테이너 리스트를 조회합니다. 

 

$ docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS       PORTS                   NAMES
980e699cd925   wordpress   "docker-entrypoint.s…"   4 weeks ago   Up 4 weeks   0.0.0.0:58755->80/tcp   wordpress
b7d989a6efb4   mysql:5.7   "docker-entrypoint.s…"   4 weeks ago   Up 4 weeks   3306/tcp, 33060/tcp     wordpressdb

 

그리고 확인하고자 하는 컨테이너의 로그를 아래 명령어를 통해 조회합니다.

 

저는 wordpressdb 컨테이너의 로그를 확인해 보았습니다.

 

$ docker logs wordpressdb
2022-03-08 16:15:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.37-1debian10 started.
2022-03-08 16:15:51+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-03-08 16:15:51+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.37-1debian10 started.
2022-03-08 16:15:51+00:00 [Note] [Entrypoint]: Initializing database files
2022-03-08T16:15:51.746840Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
....

MySQL 로그가 이것저것 나와있습니다.

 

tail 의 사용

 

길이가 너무 긴데요, 파이프로 head, tail 명령을 사용하면 원하는대로 출력이 나오지 않습니다.

 

$ docker logs wordpressdb | tail -n 1
2022-03-08T16:15:51.746840Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-03-08T16:15:52.076390Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-03-08T16:15:52.137848Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-03-08T16:15:52.147917Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 064f3d36-9efb-11ec-abc3-0242ac110002.

 

 

docker logs 명령어는 tail 옵션을 제공하기 때문에 파이프를 하지 않아도 됩니다.

docker logs --tail  옵션을 통해 마지막 로그 줄 부터 출력 라인의 갯수를 지정 가능합니다.

 

$ docker logs --tail 1 wordpressdb
2022-04-09T12:56:13.714730Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 19654ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)

 

since option

 

docker logs 명령어에  --since 옵션을 이용하녀 유닉스 시간을 입력하고 그 시간 이후의 로그를 확인할 수 있습니다.

 

$ docker logs --since 1649000000 wordpressdb
2022-04-02T15:44:42.179649Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 15847ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2022-04-06T21:59:36.087470Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 5649ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2022-04-07T02:00:11.788922Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 15953ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2022-04-09T12:56:13.714730Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 19654ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)

 

-t 옵션은 타임스탬프를 출력 해 보여줍니다.

보기가 썩 좋지만은 않습니다.

 

$ docker logs --since 1649000000 -t wordpressdb
2022-04-06T13:54:08.073069400Z 2022-04-02T15:44:42.179649Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 15847ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2022-04-06T21:59:36.087780800Z 2022-04-06T21:59:36.087470Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 5649ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2022-04-07T02:00:12.552969000Z 2022-04-07T02:00:11.788922Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 15953ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2022-04-09T12:56:15.447087800Z 2022-04-09T12:56:13.714730Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 19654ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)

 

docker logs의 각종 옵션들은 원하는 시간대의 로그를 필요한 만큼만 출력하는데 상당한 도움을 줍니다.

잘 숙지해서 사용하면 컨테이너 로그를 더 빠르고 쉽게 분석할 수 있을 것으로 보입니다.

 

사실 관련 업무를 하다보면 너무나 자주 쓸 법한 명령어라서 자연스럽게 숙달되지 않을까 싶기도 합니다.