How to write Docker logs to a file in real time (à la `tail -f`)
3
votes
2
answers
3892
views
I docker that output logs stdout stderr, which can be viewed using:
docker logs -f $LOGS_CONTAINER_ID
I also added 'sed', which puts the container id on each line:
docker logs -f $LOGS_CONTAINER_ID | sed "s/^/$LOGS_CONTAINER_ID /"
If I run it, I get something like:
container112 error 10:20:10 problem
container112 info 10:20:09 not problem
container112 error 10:20:01 problem
where "container112" is $LOGS_CONTAINER_ID.
SO FAR SO GOOD. Now I want to output the above command to a file (log.out),
so I wrote the following command:
docker logs -f $LOGS_CONTAINER_ID | sed "s/^/$LOGS_CONTAINER_ID /" >> log.out
What happens is that it writes the logs to log.out, but it doesn't get new logs (if I open a new session and run
tail -f log.out
, I don't get output).
So I also tried:
tail -f $(docker logs -f $LOGS_CONTAINER_ID | sed "s/^/$LOGS_CONTAINER_ID /") >> log.out
But it also didn't work.
What is the problem?
Asked by Yagel
(143 rep)
Mar 3, 2019, 08:09 PM
Last activity: Jun 21, 2025, 08:03 PM
Last activity: Jun 21, 2025, 08:03 PM