为 django 应用程序公开容器端口

Posted

技术标签:

【中文标题】为 django 应用程序公开容器端口【英文标题】:exposing container ports for django application 【发布时间】:2017-03-21 02:19:38 【问题描述】:

我的 django 应用程序在本地运行时运行良好,但在容器上运行时却无法运行。是端口问题吗?当我将它作为本地应用程序(没有 docker)运行时,我将它指向 localhost:8000/polls,它就像一个魅力。但是当我运行容器时,我得到 “localhost 页面不工作 localhost 没有发送任何数据。ERR_EMPTY_RESPONSE”。该应用程序几乎是来自 https://docs.djangoproject.com/en/1.10/intro/tutorial01/ 的 Django 教程的逐字记录

操作系统 Windows 10 Python 3.6 Docker 17.03.0-ce

已更新以包含 docker inspect 输出(容器 ip 地址也不起作用):

C:\Users\jonathan>docker 检查 xenodochial_swanson [ "ID": "0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f", “创建”:“2017-03-21T03:27:42.432908Z”, "路径": "/bin/sh", “Args”:[ “-C”, “python /usr/src/toplevel/manage.py 运行服务器” ], “状态”: “状态”:“运行”, “跑步”:是的, “暂停”:错误, “重启”:假, “OOMKilled”:假, “死”:假, “PID”:3415, “退出代码”:0, “错误”: ””, “开始时间”:“2017-03-21T03:27:42.933502Z”, “完成时间”:“0001-01-01T00:00:00Z” , "图像": "sha256:18be11864bbd096c71343d6621712d2dfed06a3e0bef0d72bea3e3bd97d94509", "ResolvConfPath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/resolv.conf", “主机名路径”:“/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/主机名”, "HostsPath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/hosts", "LogPath": "/var/lib/docker/containers/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f14775dfd506a7a3f/0394ca73fb06492031a43c3056bc26181a0a0d5f685b414f "名称": "/xenodochial_swanson", “重启计数”:0, “驱动程序”:“overlay2”, "安装标签": "", "进程标签": "", "AppArmorProfile": "", “执行 ID”:[ “472a4b94af64b347ff34e6d98875bf16e183d04974d7497f7c6d68469398d70e” ], “主机配置”: “绑定”:空, "ContainerIDFile": "", “日志配置”: “类型”:“json文件”, “配置”: , “网络模式”:“默认”, “端口绑定”: “8000/tcp”:[ “主机IP”:“”, “主机端口”:“8000” ] , “重启策略”: “名称”:“没有”, “最大重试次数”:0 , “自动删除”:假, “音量驱动程序”:“”, “VolumesFrom”:空, “CapAdd”:空, “CapDrop”:空, “DNS”:[], "DnsOptions": [], “DNS搜索”:[], “额外主机”:空, “组添加”:空, "IpcMode": "", “C组”:“”, “链接”:空, “OomScoreAdj”:0, "PidMode": "", “特权”:错误, “发布所有端口”:假, “ReadonlyRootfs”:假, “SecurityOpt”:空, "UTSMode": "", "用户模式": "", “ShmSize”:67108864, “运行时”:“运行”, “控制台大小”:[ 30, 120 ], “隔离”: ””, “CpuShares”:0, “记忆”:0, “NanoCpus”:0, "CgroupParent": "", “BlkioWeight”:0, “BlkioWeightDevice”:空, “BlkioDeviceReadBps”:空, “BlkioDeviceWriteBps”:空, “BlkioDeviceReadIOps”:空, “BlkioDeviceWriteIOps”:空, “CPU周期”:0, “CPU配额”:0, “CpuRealtimePeriod”:0, “CpuRealtimeRuntime”:0, "CpusetCpus": "", "CpusetMems": "", “设备”: [], "磁盘配额": 0, “内核内存”:0, “内存保留”:0, “内存交换”:0, “内存交换”:-1, “OomKillDisable”:假, “PidsLimit”:0, “极限”:空, “CPU计数”:0, “CPU百分比”:0, “IOMaximumIOps”:0, “IOMaximumBandwidth”:0 , “图形驱动程序”: "名称": "overlay2", “数据”: “LowerDir”:“的/ var / lib中/搬运工/ overlay2 / 61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af-INIT / DIFF:在/ var / lib中/搬运工/ overlay2 / 70d4d830664d3a9da7f1c4061421924fb27c547e771292961296b6f068549d94 / DIFF:在/ var / lib中/搬运工/ overlay2 / efcad5a908608693833608bb6874cd198c625e9f7dbef72a3a2165fea42c4189 / DIFF:在/ var / lib中/搬运工/ overlay2 / 8553d226e111b5d9837656d1bfef7c6e4aa7a02280d154824db3b05dded594d9 / DIFF:在/ var / lib中/搬运工/ overlay2 / de3b36b0203b5d17ecd88106a54ae87fe49d88fb1bec04cf5641c1a9d92051fd / DIFF:在/ var / lib中/搬运工/ overlay2 / 9ff4cd59d0321627d952a01c2e97f09e9b0cee6b58b69ac1ef89ce1012fc3ca0 / DIFF:在/ var / lib中/搬运工/ overlay2 / 52161c423deed2bc1a58fbb5252332c0bf68ebe4f8843ded1679f1599d9a09b6 / DIFF:/ VAR / LIB /搬运工/ overlay2 / ccf0d30cc04d29769faaaf0d3abc4c822ddea6caa62ff21a67629b74fdf5ccea / DIFF:在/ var / lib中/搬运工/ overlay2 / 9213e08c956581cdca5f99efa2632dfb1c2eb86684f7d0b4138c86a8121f43ca / DIFF:在/ var / lib中/搬运工/ overlay2 / eca56289a13e584416695b55c77907dbe40e24e726d672f5a5cf40be95521b27 / DIFF:在/ var / lib中/搬运工/ overlay2 / 52b015f 1e0d4bf4b00ec4336091f8bc6c49c1407c6f270e56aa5c2931dd18994/diff", "MergedDir": "/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af/merged", "UpperDir": "/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af/diff", “工作目录”:“/var/lib/docker/overlay2/61d1577ad2925d86ae68c5bb2140aa7b85f4d2849866a4875a8a50f35726c7af/work” , “坐骑”:[], “配置”: "主机名": "0394ca73fb06", "域名": "", “用户”:“”, “附加标准”:假, “附加标准输出”:真, “AttachStderr”:是的, “暴露端口”: “8000/tcp”: , “Tty”:是的, “OpenStdin”:假, “StdinOnce”:错误, “环境”:[ "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D", "PYTHON_VERSION=3.6.0", “PYTHON_PIP_VERSION=9.0.1” ], “命令”:[ "/bin/sh", “-C”, “python /usr/src/toplevel/manage.py 运行服务器” ], “ArgsEscaped”:是的, “图像”:“django-polls:最新”, “卷”:空, "工作目录": "", “入口点”:空, “OnBuild”:空, “标签”: , “网络设置”: “桥”: ””, "沙盒ID": "ab70160de420857c339a85aeb99a72533b2fd382606e08e39921dc3efcece6da", “发夹模式”:假, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, “端口”: “8000/tcp”:[ "HostIp": "0.0.0.0", “主机端口”:“8000” ] , "SandboxKey": "/var/run/docker/netns/ab70160de420", “辅助IP地址”:空, “辅助 IPv6 地址”:空, "EndpointID": "e52b41debee6e478c638440cdf930c1a63f7a2c5a588010cd0f47fb1eea47b05", “网关”:“172.17.0.1”, "全球IPv6地址": "", "GlobalIPv6PrefixLen": 0, "IP地址": "172.17.0.2", “IPPrefixLen”:16, "IPv6网关": "", "MacAddress": "02:42:ac:11:00:02", “网络”: “桥”: “IPAMConfig”:空, “链接”:空, “别名”:空, "NetworkID": "d494fc6cb4c7c4af4bb936d8a89087bce1883e64cfad5c34c52775125dc13555", "EndpointID": "e52b41debee6e478c638440cdf930c1a63f7a2c5a588010cd0f47fb1eea47b05", “网关”:“172.17.0.1”, "IP地址": "172.17.0.2", “IPPrefixLen”:16, "IPv6网关": "", "全球IPv6地址": "", "GlobalIPv6PrefixLen": 0, “MacAddress”:“02:42:ac:11:00:02” ]

Docker 文件:

FROM python:3.6
EXPOSE 8000
COPY ./ /usr/src/
RUN pip install -r /usr/src/requirements.txt
RUN pip install /usr/src/django-polls/dist/django-polls-0.1.tar.gz
CMD python /usr/src/toplevel/manage.py runserver 

运行容器(无明显问题):

C:\temp\djangopoc>docker run -p 8000:8000 -t django-polls
Performing system checks...
System check identified no issues (0 silenced).
March 21, 2017 - 01:05:05
Django version 1.10.6, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

疑难解答信息? (注意端口说 0.0.0.0:8000)问题?:

C:\temp\djangopoc>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d28b7c336664        django-polls        "/bin/sh -c 'pytho..."   About an hour ago   Up About an hour    0.0.0.0:8000->8000/tcp   xenodochial_boyd

如果对诊断、构建指令和输出有帮助:

C:\temp\djangopoc>docker build -t django-polls:latest .
Sending build context to Docker daemon 4.948 MB
Step 1/6 : FROM python:3.6
3.6: Pulling from library/python
Digest: sha256:438208801c4801efbf8b0e318ff6548460b27bd1fbcb7bb188273d13871ab43f
Status: Downloaded newer image for python:3.6
 ---> a1782fa44ef7
Step 2/6 : EXPOSE 8000
 ---> Running in cde33b9d347d
 ---> 34ebaa1cfd3c
Removing intermediate container cde33b9d347d
Step 3/6 : COPY ./ /usr/src/
 ---> a6b591e2fd0b
Removing intermediate container e45a71d06c9a
Step 4/6 : RUN pip install -r /usr/src/requirements.txt
 ---> Running in 9d995a9129fb
Collecting appdirs==1.4.2 (from -r /usr/src/requirements.txt (line 1))
...
Collecting numpy>=1.7.0 (from pandas==0.19.2->-r /usr/src/requirements.txt (line 23))
  Downloading numpy-1.12.1-cp36-cp36m-manylinux1_x86_64.whl (16.8MB)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/site-packages (from pyramid==1.8.2->-r /usr/src/requirements.txt (line 28))
Installing collected packages: appdirs, six, python-dateutil, arrow, docutils, colorama, PyYAML, jmespath, botocore, s3transfer, pyasn1, rsa, awscli, chardet, binaryornot, click, MarkupSafe, Jinja2, jinja2-time, whichcraft, poyo, future, cookiecutter, decorator, Django, funcsigs, hupper, mysql-connector, pyparsing, packaging, pytz, numpy, pandas, PasteDeploy, zope.deprecation, translationstring, WebOb, venusian, zope.interface, repoze.lru, pyramid
  Running setup.py install for arrow: started
    Running setup.py install for arrow: finished with status 'done'
  Running setup.py install for PyYAML: started
    Running setup.py install for PyYAML: finished with status 'done'
...
  Running setup.py install for repoze.lru: started
    Running setup.py install for repoze.lru: finished with status 'done'
Successfully installed Django-1.10.6 Jinja2-2.9.5 MarkupSafe-0.23 PasteDeploy-1.5.2 PyYAML-3.12 WebOb-1.7.1 appdirs-1.4.2 arrow-0.10.0 awscli-1.11.55 binaryornot-0.4.0 botocore-1.5.18 chardet-2.3.0 click-6.7 colorama-0.3.7 cookiecutter-1.5.1 decorator-4.0.11 docutils-0.13.1 funcsigs-1.0.2 future-0.16.0 hupper-0.4.2 jinja2-time-0.2.0 jmespath-0.9.1 mysql-connector-2.1.4 numpy-1.12.1 packaging-16.8 pandas-0.19.2 poyo-0.4.0 pyasn1-0.2.3 pyparsing-2.1.10 pyramid-1.8.2 python-dateutil-2.6.0 pytz-2016.10 repoze.lru-0.6 rsa-3.4.2 s3transfer-0.1.10 six-1.10.0 translationstring-1.3 venusian-1.0 whichcraft-0.4.0 zope.deprecation-4.2.0 zope.interface-4.3.3
 ---> dbc28b5002d5
Removing intermediate container 9d995a9129fb
Step 5/6 : RUN pip install /usr/src/django-polls/dist/django-polls-0.1.tar.gz
 ---> Running in 27d40c4a746a
Processing /usr/src/django-polls/dist/django-polls-0.1.tar.gz
Installing collected packages: django-polls
  Running setup.py install for django-polls: started
    Running setup.py install for django-polls: finished with status 'done'    
Successfully installed django-polls-0.1
 ---> 1771f5ff2479
Removing intermediate container 27d40c4a746a
Step 6/6 : CMD python /usr/src/toplevel/manage.py runserver
 ---> Running in d754737737ec
 ---> 18be11864bbd
Removing intermediate container d754737737ec
Successfully built 18be11864bbd
SECURITY WARNING: You are building a Docker image from Windows against a
 non-Windows Docker host. All files and directories added to build context
 will have '-rwxr-xr-x' permissions. It is recommended to double check and
 reset permissions for sensitive files and directories.

【问题讨论】:

我猜你的容器一定是使用了不同ip的docker机器,尝试使用docker-machine ip default检查,其中default是docker机器名称 尝试使用 0.0.0.0:8000 打开您的首选浏览器 试过 0.0.0.0:8000 但没有用。还尝试了 docker-machine ip [default] 。我在那里输入的几乎所有内容(机器名、IP 地址、0:0:0:0、128.0.0.1)都说“主机不存在” 【参考方案1】:

在 docker 机器上运行应用程序时,您应该使用 docker-machine ip 而不是 localhost

使用:docker-machine ip default 获取 docker 机器 ip,其中 default 是您的机器

【讨论】:

我不确定我在用这个命令做什么,但我试过了,它出错了。我为 0.0.0.0 和 128.0.0.1 尝试了这个相同的错误 C:\temp\djangopoc>docker-machine ip 192.168.1.90 主机不存在:“192.168.1.90”。 如果您正在运行docker run -p 8000:8000 -t django-polls,则使用192.168.1.90:8000 docker-machine ls 应该列出 docker 机器 当我输入它时,它看起来好像我没有任何机器?什么都没有返回。 C:\temp\djangopoc>docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS 我猜你的系统上没有创建任何机器,请在 docker machine 上查看更多信息【参考方案2】:

您是否尝试过查看运行 django 应用程序的容器内的日志?要进入容器,请使用docker exec -it django-polls bash

【讨论】:

试了一下,得到了这个反馈:C:\temp\djangopoc>docker exec -it django-polls bash 来自守护进程的错误响应:没有这样的容器:django-polls 你是在容器运行的时候做的吗?无论您运行 docker ps 时的图像名称是什么,您都应该使用它来代替 django-polls 啊,明白了。我在壳里。该应用程序不做任何日志记录,但我从 shell 执行了测试算法,它通过了所有测试。【参考方案3】:

也许您可以尝试使用以下命令:

CMD python /usr/src/toplevel/manage.py runserver 0.0.0.0:8000

【ref1】What is the difference between 0.0.0.0, 127.0.0.1 and localhost?

【讨论】:

以上是关于为 django 应用程序公开容器端口的主要内容,如果未能解决你的问题,请参考以下文章

sh 在Docker容器中启动MongoDB并检查公开的端口

为 Cloud Run 部署公开 8080 和 3000 端口

如何仅将 Docker 容器端口公开给 localhost,以便也可以通过 ssh 隧道访问它?

Docker wordpress 多站点 - 要公开哪些端口?

Rancher - 如何公开我的服务?

Django私人帖子和公开帖子的例子?