program tip

Docker 컨테이너 로그를 단일 파일로 리디렉션하는 방법은 무엇입니까?

radiobox 2020. 9. 23. 07:30
반응형

Docker 컨테이너 로그를 단일 파일로 리디렉션하는 방법은 무엇입니까?


Docker 컨테이너의 모든 로그를 단일 로그 파일로 리디렉션하여 분석하고 싶습니다. 나는 시도했다

docker logs container > /tmp/stdout.log 2>/tmp/stderr.log

그러나 이것은 두 개의 다른 파일에 로그를 제공합니다. 나는 이미 시도했다

docker logs container > /tmp/stdout.log

하지만 작동하지 않았습니다.


로그를 리디렉션 할 필요가 없습니다.

Docker는 기본적으로 로그를 하나의 로그 파일에 저장합니다. 로그 파일 경로를 확인하려면 명령을 실행하십시오.

docker inspect --format='{{.LogPath}}' containername

/var/lib/docker/containers/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334-json.log

해당 로그 파일을 열고 분석하십시오.

로그를 리디렉션하면 리디렉션 전에 만 로그를 받게됩니다. 라이브 로그를 볼 수 없습니다.

편집하다:

라이브 로그를 보려면 아래 명령을 실행할 수 있습니다.

tail -f `docker inspect --format='{{.LogPath}}' containername`

노트 :

이 로그 파일 /var/lib/docker/containers/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334-json.log은 로그가없는 경우도 커가 로그를 생성하는 경우에만 생성되며이 파일은 존재하지 않습니다. 우리가 명령을 실행 docker logs containername하고 아무 것도 반환하지 않는 경우와 비슷 합니다. 이 시나리오에서는이 파일을 사용할 수 없습니다.


이 옵션은 어떻습니까?

docker logs containername >& logs/myFile.log

질문에서 요청한 로그를 리디렉션하지 않고 특정 파일에 한 번 복사합니다.


docker logs -f <yourContainer> &> your.log &

설명:

  • -f(즉 --follow) : 기존의 모든 로그를 기록하고 계속 ( 다음 다음 온다 로깅 다).
  • &> 표준 출력과 표준 오류를 모두 리디렉션합니다.
  • 아마도 백그라운드에서 해당 메서드를 실행하고 싶을 것 &입니다.
  • 다음 > output.log 2> error.log을 사용하여 출력과 stderr을 분리 할 수 ​​있습니다 (를 사용하는 대신 &>).

컨테이너가 여러 개 있고 로그를 단일 파일로 집계하려는 경우 fluentd와 같은 로그 집계기를 사용해야합니다. fluentd는 도커 컨테이너의 로깅 드라이버로 지원됩니다.

따라서 docker-compose에서 로깅 드라이버를 정의해야합니다.

  service1:
    image: webapp:0.0.1
    logging:
      driver: "fluentd"
      options:
        tag: service1 

  service2:
        image: myapp:0.0.1
        logging:
          driver: "fluentd"
          options:
            tag: service2

두 번째 단계는 서비스 1과 서비스 2 모두에 대한 로그를 제공하도록 fluentd conf를 업데이트하는 것입니다.

 <match service1>
   @type copy
   <store>
    @type file
    path /fluentd/log/service/service.*.log
    time_slice_format %Y%m%d
    time_slice_wait 10m
    time_format %Y%m%dT%H%M%S%z
  </store>
 </match> 
 <match service2>
    @type copy
   <store>
    @type file
    path /fluentd/log/service/service.*.log
    time_slice_format %Y%m%d
    time_slice_wait 10m
    time_format %Y%m%dT%H%M%S%
  </store>
 </match> 

In this config, we are asking logs to be written to a single file to this path
/fluentd/log/service/service.*.log

and the third step would be to run the customized fluentd which will start writing the logs to file.

Here is the link for step by step instructions

Bit Long, but correct way since you get more control over log files path etc and it works well in Docker Swarm too .


To capture both stdout & stderr from your docker container to a single log file run the following:

docker logs container > container.log 2>&1

Since Docker merges stdout and stderr for us, we can treat the log output like any other shell stream. To redirect the current logs to a file, use a redirection operator

$ docker logs test_container > output.log
docker logs -f test_container > output.log

Instead of sending output to stderr and stdout, redirect your application’s output to a file and map the file to permanent storage outside of the container.

$ docker logs test_container> /tmp/output.log

Docker will not accept relative paths on the command line, so if you want to use a different directory, you’ll need to use the complete path.


If you work on Windows and use PowerShell (like me), you could use the following line to capture the stdout and stderr:

 docker logs <containerId> | Out-File 'C:/dev/mylog.txt'

I hope it helps someone!


Bash script to copy all container logs to a specified directory:

#!/usr/bin/env bash

TARGET_DIR=~/logs/docker_logs
mkdir -p "$TARGET_DIR"
for name in `sudo docker ps --format '{{.Names}}'`;
do
    path=$(sudo docker inspect --format='{{.LogPath}}' $name)
    sudo cp -rf "$path" "$TARGET_DIR"/$name.log
done

참고URL : https://stackoverflow.com/questions/41144589/how-to-redirect-docker-container-logs-to-a-single-file

반응형