How to write Docker logs to a file in real time (à la `tail -f`)












0















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?










share|improve this question

























  • 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 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 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

    – Stephen Harris
    Mar 3 at 21:20











  • 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 at 22:58
















0















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?










share|improve this question

























  • 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 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 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

    – Stephen Harris
    Mar 3 at 21:20











  • 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 at 22:58














0












0








0








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?










share|improve this question
















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?







bash shell pipe docker tail






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 3 at 21:07









G-Man

13.6k93769




13.6k93769










asked Mar 3 at 20:09









Yagel Yagel

1




1













  • 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 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 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

    – Stephen Harris
    Mar 3 at 21:20











  • 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 at 22:58



















  • 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 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 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

    – Stephen Harris
    Mar 3 at 21:20











  • 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 at 22:58

















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 at 20:15





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 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 at 20:55





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 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

– Stephen Harris
Mar 3 at 21:20





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

– Stephen Harris
Mar 3 at 21:20













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 at 22:58





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 at 22:58










0






active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504145%2fhow-to-write-docker-logs-to-a-file-in-real-time-%25c3%25a0-la-tail-f%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504145%2fhow-to-write-docker-logs-to-a-file-in-real-time-%25c3%25a0-la-tail-f%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

第一次世界大戦

Touch on Surface Book