Save docker-compose logs to a file

后端 未结 5 543
情歌与酒
情歌与酒 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.

提交回复
热议问题