Docker 로그를 확인하는 방법?

게시 됨: 2021-08-30

Docker는 널리 사용되는 OS 수준 가상화 플랫폼으로, 간단히 말해 자체 소프트웨어, 라이브러리 및 구성 파일이 있는 격리된 환경인 컨테이너로 알려진 패키지로 애플리케이션을 제공할 수 있습니다.

다른 최신 소프트웨어와 마찬가지로 경고 및 오류와 같은 이벤트 및 메시지 로깅은 애플리케이션 및 프로덕션 문제를 디버깅할 수 있는 Docker 플랫폼의 고유한 부분입니다.

컨테이너의 로그를 관리하고 모니터링할 수 있는 몇 가지 간단한 방법을 다룰 것입니다. 시작하겠습니다.

Docker 로그 명령

컨테이너의 로그를 가져오는 기본 구문은 다음과 같습니다.

 $ 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분 동안 56m) 이후의 로그 표시
--tail , -n all 로그 끝에서 표시할 줄 수
--timestamps, -t 타임스탬프 표시
--until API 1.35+
타임스탬프(예: 2021-08-28T15:23:37Z) 또는 상대적(예: 56분 동안 56m) 이전에 로그 표시

예시:

 $ 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 플래그를 사용하십시오.

예시:

 $ 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 플래그를 사용하여 로그 출력을 따를 수 있습니다. 이를 통해 지속적으로 STDOUTSTDERR 에서 로그 스트림의 새 업데이트를 모니터링할 수 있습니다.

예시:

 $ 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 플래그를 사용하여 화면에 표시되는 출력 수를 제한하도록 꼬리를 지정할 수 있습니다. 기본적으로 이 플래그는 전체 로그 스트림을 표시하는 인수로 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 또는 56분 동안 56m 과 같은 상대 값과 같은 타임스탬프를 제공하여 로그 출력을 제한할 수 있습니다.

--since 옵션은 지정된 날짜 이후에 생성된 컨테이너 로그만 표시합니다. 날짜를 RFC 3339 날짜, UNIX 타임스탬프 또는 Go 기간 문자열(예: 1m30s , 3h )로 지정할 수 있습니다. 타임스탬프 끝에 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" "-" $

위의 예에서는 nostalgic_wescoffnginx 컨테이너에 할당된 자동 생성 이름인 2분 이후의 로그가 표시됩니다.

다음까지 로그 표시

마찬가지로 --since 플래그, docker logs 도 지원 --until 쇼 로그 주어진 타임 스탬프 전에 플래그. 마찬가지로 타임스탬프는 이전과 유사한 규칙을 2021-08-28T15:23:37Z 구문을 사용하여 절대값으로 지정하거나 56분 동안 56m 과 같은 상대값으로 지정할 수 있습니다.

예시:

 $ 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 셸 유틸리티를 사용하여 로그 출력을 보다 쉽게 ​​작성할 수도 있습니다. grep , head , tail 등과 같은 유틸리티는 고급 작업을 위해 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는 환경을 관리하기 위한 다양한 기능을 제공하는 다목적 플랫폼입니다. 시스템의 로그 관리는 모든 시스템 관리자가 알아야 하는 필수 기술 중 하나입니다. 요구 사항에 따라 사용 가능한 명령과 가능한 플래그를 알면 Docker에서 로그를 쉽게 관리할 수 있습니다.

Docker 및 해당 기능에 대한 자세한 내용은 Docker 설명서를 참조하십시오.