Ivan Talalaev
Ivan Talalaev

Reputation: 6494

Ho to write logs in Docker container

I made most simple C application which just printing a message:

#include <stdio.h>
#include <unistd.h>
int main(){
    int i = 0;
    while(1) {
        sleep(1);
        printf("sleeping ... %d\n", i++);
    }
}

Then, made a simplest possible Dockerfile for it:

FROM fedora

USER root
WORKDIR /root

ADD sleep-play .

CMD ["/root/sleep-play"]

Then, run that container as simple as it could:

sudo docker build -t sleeping .
sudo docker run --name sleeping -d sleeping

Finally I'm expecting to see my stdout output, but nothing happens:

sudo docker logs -f sleeping
## nothing shown!

From here:

... By default, docker logs shows the command’s STDOUT and STDERR.

What I'm doing wrong?

Upvotes: 4

Views: 3567

Answers (2)

Hannes
Hannes

Reputation: 501

I reproduced it and figured out that the sleep command is your problem. At the moment i dont know the exactly cause of this but without the sleep command it runs.

Maybe this article can help you: https://github.com/moby/moby/issues/2838#issuecomment-194054757

Upvotes: 1

snap
snap

Reputation: 1915

I'm not quite sure, my best guess is that printf is buffered and maybe the buffer will not flush automaticly here.

Try to flush it manually:

fflush(stdout)

See also this related Stackoverflow entry for flushing manual printf statements: write() to stdout and printf output not interleaved?

Upvotes: 1

Related Questions