Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django REST API作为Docker映像运行时挂起_Docker_Django Rest Framework - Fatal编程技术网

Django REST API作为Docker映像运行时挂起

Django REST API作为Docker映像运行时挂起,docker,django-rest-framework,Docker,Django Rest Framework,当不是以Docker映像运行时,一切正常 Dockerfile: FROM python:3.5.3-slim RUN mkdir /django-rest-api WORKDIR /django-rest-api ADD . /django-rest-api RUN pip install -r requirements.txt EXPOSE 8000 CMD ["python", "./djangorest/manage.py", "runserver"] 生成命令: docker bu

当不是以Docker映像运行时,一切正常

Dockerfile:

FROM python:3.5.3-slim
RUN mkdir /django-rest-api
WORKDIR  /django-rest-api
ADD . /django-rest-api
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python", "./djangorest/manage.py", "runserver"]
生成命令:

docker build -t django-rest-api .
运行命令:

docker run -p 8000:8000 django-rest-api
manage.py:

import os
import sys



print('lol') # <------- the only change from the original file



if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangorest.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

是什么让它等待中断?

运行此容器时,我希望在标准输出上生成以下内容:

lol
Performing system checks...

System check identified no issues (0 silenced).
July 14, 2017 - 22:14:35
Django version 1.10.2, using settings 'djangorest.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
这大约是250字节的长度

或者,如果您的服务器没有写入调试输出,则可能只有
lol\n
,即4个字节

您在
docker run
命令中未使用
-ti
,因此容器未连接终端,并且未设置为以交互模式运行。在这种情况下,Docker将使用缓冲输出。本例中的标准Linux行为是等待缓冲区累积4096字节(4KB),然后将缓冲区刷新为标准输出

但是,由于到目前为止只输出了250(或者可能只有4)字节,因此缓冲区仍处于挂起状态。因此,输出中没有写入任何内容供您查看。如果您在端口8000上向服务器发出一些请求,可能很快就会达到4096字节,并导致写入输出


当您发送SIGINT(CtrlC)时,这一切都会短路,因为进程结束,所以缓冲区关闭并写入输出。

如果您将
-ti
添加到run命令,它是否也会这样做?(
docker run-ti…
)@DanLowe谢谢!但是我不明白为什么,即使我读到了关于-I和-tIf的内容,如果您没有使用
-ti
将终端连接到会话,那么它就不是处于交互模式,它会缓冲输出。在这种情况下,只有当缓冲区达到4kb时,它才会将缓冲区转储到stdout(您的终端)。这是标准的Linux i/o行为。但是,当你^C(SIGINT)时,你强制了缓冲区转储(可能还杀死了容器)。@DanLowe我明白了,thx太多了!
lol
Performing system checks...

System check identified no issues (0 silenced).
July 14, 2017 - 22:14:35
Django version 1.10.2, using settings 'djangorest.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.