Save docker-compose logs to a file

2019-03-09 00:13发布

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 docker-compose logs to a file, or to find where the log files themselves actually live.

I want the equivalent of:

docker-compose logs > logs.txt

Edit - clarification:

All of my docker containers produce useful logs, which a manual run of docker-compose logs reveals. I want to script this process to save those same logs to a file that is an artifact on my build server. Essentially, the output of docker-compose logs saved to a file, however docker-compose logs never exits.

4条回答
时光不老,我们不散
2楼-- · 2019-03-09 00:40

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

查看更多
beautiful°
3楼-- · 2019-03-09 00:47

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 CONTAINER_NAME > logs.txt

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

查看更多
来,给爷笑一个
4楼-- · 2019-03-09 00:52

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.

查看更多
我想做一个坏孩纸
5楼-- · 2019-03-09 01:03

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
查看更多
登录 后发表回答