docker run,docker exec和日志 [英] docker run, docker exec and logs
问题描述
如果我这样做:
docker run --name nginx -d nginx:alpine /bin/sh -c 'echo "Hello stdout" > /dev/stdout'
当我这样做时,我可以看到"Hello stdout":
I can see "Hello stdout" when I do :
docker logs nginx
但是当容器运行时(docker run --name nginx -d nginx:alpine),我这样做了:
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'
或当我将容器附加到:
docker exec -it nginx /bin/sh
然后:
echo "Hello stdout" > /dev/stdout
我在Docker日志中什么都看不到.而且由于我的Nginx访问日志已重定向到/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 ?
推荐答案
当您 docker exec
时,您会看到有多个进程
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
/ #
在Linux中,每个进程在/proc/pid/fd中都有自己的stdin,stdout,stderr(和其他文件描述符)
and in Linux, each process has its own stdin, stdout, stderr (and other file descriptors), in /proc/pid/fd
,因此,使用您的 docker exec
(pid 7),您将在其中显示某些内容
and so, with your docker exec
(pid 7) you display something in
/proc/7/fd/1
/proc/7/fd/1
如果执行 ls -ltr/proc/7/fd/1
,则会显示类似/proc/4608/fd/1->/dev/pts/2
表示输出正在发送到终端
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
您的nginx进程(pid 1)在其中显示其输出
while your nginx process (pid 1) displays his output in
/proc/1/fd/1
/proc/1/fd/1
如果执行 ls -ltr/proc/1/fd/1
,则显示类似/proc/1/fd/1->的内容.管道:[184442508]
,这意味着输出正在发送到Docker日志记录驱动程序
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
这篇关于docker run,docker exec和日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!