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.
You find these JSON log files in the /var/lib/docker/containers/ directory on a Linux Docker host.
Redirect Docker Logs to File 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. To send the current logs and then any updates that follow, use –follow with the redirection operator.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With