How to write Docker logs to a file in real time (à la `tail -f`)
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
add a comment |
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
Why would it have the new logs? Are you running something in the background? Yourdocker logscommand 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 dodocker inspect $CONTAINER_ID | grep logthen 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 makingsedunbuffered (either using-uor thestdbufhelper command) will fix your issue.
– filbranden
Mar 3 at 22:58
add a comment |
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
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
bash shell pipe docker tail
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? Yourdocker logscommand 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 dodocker inspect $CONTAINER_ID | grep logthen 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 makingsedunbuffered (either using-uor thestdbufhelper command) will fix your issue.
– filbranden
Mar 3 at 22:58
add a comment |
Why would it have the new logs? Are you running something in the background? Yourdocker logscommand 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 dodocker inspect $CONTAINER_ID | grep logthen 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 makingsedunbuffered (either using-uor thestdbufhelper 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Why would it have the new logs? Are you running something in the background? Your
docker logscommand 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 logthen 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
sedunbuffered (either using-uor thestdbufhelper command) will fix your issue.– filbranden
Mar 3 at 22:58