2

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?

4
  • Why would it have the new logs? Are you running something in the background? Your docker logs command only runs once. If you want more logs, you need to run it again.
    – terdon
    Mar 3, 2019 at 20:15
  • if I just run "docker logs -f $LOGS_CONTAINER_ID >> log.out", and on another session I run "tail -f log.out", I get new logs.
    – Yagel
    Mar 3, 2019 at 20:55
  • If you do docker inspect $CONTAINER_ID | grep log then you'll find a pathname where docker writes its logs to. You may be able to use that Mar 3, 2019 at 21:20
  • 1
    Possible duplicate of stackoverflow.com/q/577865/9447571, see if making sed unbuffered (either using -u or the stdbuf helper command) will fix your issue.
    – filbranden
    Mar 3, 2019 at 22:58

1 Answer 1

0

Would this help?

docker logs $CONTAINERID 2>&1 | cat > logfile.log

1
  • 1
    Perhaps docker logs $CONTAINERID > logfile.log 2>&1? Feb 14, 2020 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .