I want to redirect all the logs of my docker container to single log file to analyse them. I tried
docker logs container > /tmp/stdout.log 2>/tmp/stderr.log
but this gives log in two different file. I already tried
docker logs container > /tmp/stdout.log
but it did not work.
No need to redirect logs.
Docker by default store logs to one log file. To check log file path run command:
Open that log file and analyse.
if you redirect logs then you will only get logs before redirection. you will not be able to see live logs.
EDIT:
To see live logs you can run below command
Note:
This log file
/var/lib/docker/containers/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334-json.log
will be created only if docker generating logs if there is no logs then this file will not be there. it is similar like sometime if we run commanddocker logs containername
and it returns nothing. In that scenario this file will not be available.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
$ docker logs test_container > output.log
docker logs -f test_container > output.log
Instead of sending output to stderr and stdout, redirect your application’s output to a file and map the file to permanent storage outside of the container.
$ docker logs test_container> /tmp/output.log
Docker will not accept relative paths on the command line, so if you want to use a different directory, you’ll need to use the complete path.
To capture both stdout & stderr from your docker container to a single log file run the following:
Bash script to copy all container logs to a specified directory:
Assuming that you have multiple containers and you want to aggregate the logs into a single file, you need to use some log aggregator like fluentd. fluentd is supported as logging driver for docker containers.
So in docker-compose, you need to define the logging driver
The second step would be update the fluentd conf to cater the logs for both service 1 and service 2
In this config, we are asking logs to be written to a single file to this path
/fluentd/log/service/service.*.log
and the third step would be to run the customized fluentd which will start writing the logs to file.
Here is the link for step by step instructions
Bit Long, but correct way since you get more control over log files path etc and it works well in Docker Swarm too .
docker logs -f <yourContainer> &> your.log &
Explanation:
-f
(i.e.--follow
): writes all existing logs and continues (follows) logging everything that comes next.&>
redirects both the standard output and standard error.&
.> output.log 2> error.log
(instead of using&>
).