Save docker-compose logs to a file

后端 未结 5 544
情歌与酒
情歌与酒 2020-12-12 23:57

I have unit tests running on my build server and would like to capture the log results for analysis when something fails. I have yet to find a way to redirect the output of

相关标签:
5条回答
  • 2020-12-13 00:23

    The solution I made is based on Chris McKinnel's answer with some enhancements.

    I needed to dump last 1MIO records from my nginx container and docker-compose logs --tail 1000000 nginx > last1mio.log was too slow and the output was not exactly the same as usual nginx files have.

    This is solution that works for me:

    docker inspect --format='{{.LogPath}}' nginx-1 \
    | xargs tail -n 1000000 \
    | jq -r -j .log > last1mio.log 2>&1
    

    Notes:

    • you need to know your container name, in my case is nginx-1 - I use docker-compose option container_name: for having this constant
    • I am using JQ utility to parse json - with some special options to get the format the same as original nginx log files usually have.

    In my case dump of last 1mio lines of nginx log takes cca 10s.

    0 讨论(0)
  • 2020-12-13 00:33

    I am not sure what you are trying to achieve. Are you trying to reproduce

    docker-compose logs > logs.txt
    

    within the compose file as an instruction? Or is your issue that the redirect does not "catch" the whole output?

    In the later, you can do:

    docker-compose logs --no-color >& logs.txt
    

    Or

    docker-compose logs --no-color |& tee logs.txt
    

    to both see the logs on the terminal and dump it to a file at the same time.

    0 讨论(0)
  • 2020-12-13 00:35

    In later release docker-compose 1.7.x+, it is fixed. see https://github.com/docker/compose/issues/2227 & https://github.com/docker/compose/releases/tag/1.7.0

    Before that, there is another way to achieve it, the solution of accepted answer is to access host files directly, Which may be not applicable for remote/security case.

    Below we can get the container name from docker-compose ps command and let the docker logs command to loop

    docker-compose ps | tail -n +3 | awk '{print $1}' | xargs -n1 docker logs
    
    0 讨论(0)
  • 2020-12-13 00:38

    By default docker uses the json-file driver to record your containers logs and the raw json output of the logs can be found in:

    /var/lib/docker/containers/[container-id]/[container-id]-json.log
    

    You can get this location by running:

    docker inspect --format='{{.LogPath}}' [container-id or container-name]
    

    When you run docker-compose logs [service-name], docker-compose will attach to the service (container) you reference and the LogPrinter object will output the contents of the above file, but formatted so they're easier to read.

    Related docs: https://docs.docker.com/compose/compose-file/#logging

    0 讨论(0)
  • 2020-12-13 00:45

    The docker-compose logs command does terminate (unless you add the --follow setting).

    The likely problem here is that the logs are so large it takes some time to format and output them. You can reduce this issue if you limit the output by specifying either/both the number of lines you want to read and the container you want to read from.

    Try:

    docker-compose logs --no-color --tail=1000 <service-name> > logs.txt
    

    NOTE: service-name is name of service taken from docker-compose file - not container name or container id

    (Adjust the tail number as required. I've added "--no-color" to simplify the output but it is not needed.)

    0 讨论(0)
提交回复
热议问题