使用docker / fig运行Django开发服务器时,为什么某些日志输出被隐藏? [英] When running a Django dev server with docker/fig, why is some of the log output hidden?
问题描述
我正在编写一个Dockerfile,该文件需要作为 CMD
指令的一部分运行多个命令,我认为执行此操作的正确方法是运行Shell脚本通过 exec
执行主守护程序。不幸的是,作为该过程的一部分,我的一些输出(stdout或stderr?我不知道,我也不知道如何找出答案)会丢失。
I'm writing a Dockerfile which needs to run multiple commands as part of the CMD
instruction and I thought the right way to do this would be to run a shell script with the main daemon executed via exec
. Unfortunately, as part of that process some of my output (stdout? stderr? I don't know, and I don't know how to find out) gets lost.
这是shell脚本:
#!/bin/sh
python manage.py migrate
exec python manage.py runserver 0.0.0.0:8000
这个想法是 migrate
命令仅运行一次并显示其输出,然后 runserver
命令应接管容器运行,直到该过程
The idea being that the migrate
command is just run once and its output shown, and then the runserver
command should take over and the container runs until that process exits.
实际的问题是 migrate
的输出正确显示,但立即输出 runserver
未显示。奇怪的是,稍后显示 runserver
的请求记录就很好了。
The actual problem is that the output of migrate
is displayed correctly, but the immediate output of runserver
is not shown. Strangely, later request logging of runserver
is shown just fine.
为了澄清,这是我期望的输出:
To clarify, here's the output I expected:
[...]
No migrations to apply.
[...]
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[21/Jan/2015 16:27:06] "GET / HTTP/1.1" 200 15829
这就是我用 fig up
得到的东西:
Here's what I'm getting with fig up
:
[...]
No migrations to apply.
[...]
[21/Jan/2015 16:27:06] "GET / HTTP/1.1" 200 15829
我什至不确定这是谁的错。 runserver
命令是否根据其运行方式更改其输出? exec
是否有问题?是docker / fig吗?
I'm not even sure who's fault this is. Does the runserver
command change its output depending on how it is run? Is it a problem with exec
? Is it docker/fig?
作为一个额外的数据点,我注意到在使用 fig run web运行容器时,确实获得了所有输出
,但是当我做 fig up
时却没有,但是我不知道这有什么不同或相关。
As one additional data point, I noticed that I do get all the output when running the container with fig run web
, but not when I do fig up
, but I don't understand how that's different or relevant.
注意:对于标记垃圾邮件,很抱歉,一旦我知道造成这种影响的原因,我将减少标记。
Note: sorry for the tag spam, I'll reduce the tags once I know what actually causes this effect.
推荐答案
我今天使用docker composer找到了这个老问题。 Python日志记录模块检查输出是否为终端,因此您需要向服务添加 tty:true
。示例:
I found this old issue today using docker composer. Python logging module checks the output is a terminal so you need to add tty: true
to the service. Example:
version: '2'
services:
django:
tty: true
command: python -u manage.py runserver 0.0.0.0:8080
ports:
- "8080:8080"
这篇关于使用docker / fig运行Django开发服务器时,为什么某些日志输出被隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!