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.
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:You can get this location by running:
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
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:
(Adjust the tail number as required. I've added "--no-color" to simplify the output but it is not needed.)
I am not sure what you are trying to achieve. Are you trying to reproduce
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:
Or
to both see the logs on the terminal and dump it to a file at the same time.
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 thedocker logs
command to loop