Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker run, docker exec and logs

If I do :

docker run --name nginx -d nginx:alpine /bin/sh -c 'echo "Hello stdout" > /dev/stdout'

I can see "Hello stdout" when I do :

docker logs nginx

But when the container is running (docker run --name nginx -d nginx:alpine) and I do :

docker exec nginx /bin/sh -c 'echo "Hello stdout" > /dev/stdout'

or when I attach the container with :

docker exec -it nginx /bin/sh

and then :

echo "Hello stdout" > /dev/stdout

I can't see anything in docker logs. And since my Nginx access logs are redirected to /dev/stdout, I can't see them as well.

What is happening here with this stdout ?

like image 773
Tristan Avatar asked Mar 22 '17 17:03

Tristan


1 Answers

When you docker exec you can see you have several process

/ # ps -ef
PID   USER     TIME   COMMAND
    1 root       0:00 nginx: master process nginx -g daemon off;
    6 nginx      0:00 nginx: worker process
    7 root       0:00 /bin/sh
   17 root       0:00 ps -ef
/ # 

and in Linux, each process has its own stdin, stdout, stderr (and other file descriptors), in /proc/pid/fd

and so, with your docker exec (pid 7) you display something in

/proc/7/fd/1

If you do ls -ltr /proc/7/fd/1, it displays something like /proc/4608/fd/1 -> /dev/pts/2 which means output is being sent to terminal

while your nginx process (pid 1) displays his output in

/proc/1/fd/1

If you do ls -ltr /proc/1/fd/1, it displays something like /proc/1/fd/1 -> pipe:[184442508] which means output is being sent to docker logging driver

like image 53
user2915097 Avatar answered Oct 22 '22 00:10

user2915097