0

I am containerizing a simple hello world C program in docker. However, I am unable to see any docker log despite adding printf, fprints and even syscall. As it is mentioned in the link https://github.com/docker-library/hello-world/blob/master/hello.c What am I missing here?

#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>

int main() 
{
  printf("hello world\n");
  syscall(SYS_write, STDOUT_FILENO, "hello world", sizeof("hello world") - 1);
}

docker cmds used

sudo docker build -t hwsha .
sudo docker run --name hwsha-pr  hwsha
sudo docker logs hwsha-pr

my dockerfile

FROM ubuntu:latest
RUN apt-get update && apt-get install -y gcc vim 
COPY hello.c .
RUN  gcc hello.c -o hello
CMD ["./hello"]

docker inspect output

/home/ravi/>sudo docker inspect 7061172c55c1 | grep log
        "LogPath": "/var/lib/docker/containers/7061172c55c12d88778b021b6c72f617b25a0eaa770605c678c5efe409afd043/7061172c55c12d88778b021b6c72f617b25a0eaa770605c678c5efe409afd043-json.log",

/home/ravi/>sudo cat /var/lib/docker/containers/7061172c55c12d88778b021b6c72f617b25a0eaa770605c678c5efe409afd043/7061172c55c12d88778b021b6c72f617b25a0eaa770605c678c5efe409afd043-json.log
/home/ravi/>

cmd outpouts

/home/ravi/>sudo docker build -t hwsha .
Sending build context to Docker daemon  4.096kB
Step 1/5 : FROM ubuntu:latest
 ---> 74f2314a03de
Step 2/5 : RUN apt-get update && apt-get install -y gcc vim
 ---> Using cache
 ---> d8006c91e13d
Step 3/5 : COPY hello.c .
 ---> Using cache
 ---> a5060319d17a
Step 4/5 : RUN  gcc hello.c -o hello
 ---> Using cache
 ---> 684a11ee4cfb
Step 5/5 : CMD ["./hello"]
 ---> Using cache
 ---> e6bde96f1091
Successfully built e6bde96f1091
Successfully tagged hwsha:latest
/home/ravi/>sudo docker run --name hwsha-pr -d hwsha
d9ae3fd4ac383a290101d99c18be7d681acd065b3db66e454aa98299cb5a43c7
/home/ravi/>
/home/ravi/>
/home/ravi/>sudo docker logs -t hwsha-pr    ====> no logs at this point
/home/ravi/>

1 Answers1

0

Now I am able to fix this issue. The fix is to use debug logs with fprintf() using stderr. By default, only std error will get printed with docker log.

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
   printf("Hello World!....1\n");
   fprintf(stderr, "Hello World!....2\n");     # only this will be seen in docker log by default
   fprintf(stdout, "Hello World!....3\n");
   sleep(100000000);
   return 0;
}