gpt4 book ai didi

python - 无法访问 docker 容器中的 flask 应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:37 24 4
gpt4 key购买 nike

我一直在尝试在 docker 容器中创建一个 flask 应用程序。该应用程序在通过 Flask 开发服务器运行时完美运行,包括从网络上的其他机器访问网页。

我一直在尝试为该应用构建一个 Docker 容器。问题是我无法访问任何网页。我收到 500 个错误。

我的一些设置:

运行.py

#!flask_v1/bin/python3
from app import app
app.run(debug=True, host='0.0.0.0')

其中 flask_v1 是 virtualenv 目录。

docker 文件

FROM python:3.4
COPY . /site
workdir /site
RUN pip install -r requirements.txt
ENV NAME sitename
CMD ["python", "./run.py"]

我从这个版本中删除了 EXPOSE 5000,但它出现了同样的问题。它最初放在 pip 线下。

需求.txt

click==6.7
CouchDB==1.1
docutils==0.14
Flask==0.12.2
Flask-CouchDB==0.2.1
flask-dynamo==0.1.2
Flask-Login==0.4.0
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.9.6
jmespath==0.9.3
MarkupSafe==1.0
python-dateutil==2.6.1
PyYAML==3.12
s3transfer==0.1.11
six==1.11.0
Werkzeug==0.12.2
WTForms==2.1

然后我按如下方式运行构建命令(将 dockername 替换为实际名称):

docker build -t dockername .

构建完成,我按如下方式运行图像:

docker run -p 5000:5000 dockername

尝试连接到位于 localhost:5000/index(应用程序的“主页”)的应用程序,或只是 localhost:5000,或 127.0.0.1:5000/index,或 [host_ip]:5000/index,或[container_ip]:5000/index 都产生了同样的错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1610, in full_dispatch_request
rv = self.preprocess_request()
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1831, in preprocess_request
rv = func()
File "/usr/local/lib/python3.4/site-packages/flaskext/couchdb.py", line 154, in request_start
self.sync(current_app)
File "/usr/local/lib/python3.4/site-packages/flaskext/couchdb.py", line 131, in sync
if db_name not in server:
File "/usr/local/lib/python3.4/site-packages/couchdb/client.py", line 96, in __contains__
self.resource.head(name)
File "/usr/local/lib/python3.4/site-packages/couchdb/http.py", line 549, in head
return self._request('HEAD', path, headers=headers, **params)
File "/usr/local/lib/python3.4/site-packages/couchdb/http.py", line 581, in _request
credentials=self.credentials)
File "/usr/local/lib/python3.4/site-packages/couchdb/http.py", line 289, in request
conn = self.connection_pool.get(url)
File "/usr/local/lib/python3.4/site-packages/couchdb/http.py", line 507, in get
conn.connect()
File "/usr/local/lib/python3.4/http/client.py", line 871, in connect
self.timeout, self.source_address)
File "/usr/local/lib/python3.4/socket.py", line 516, in create_connection
raise err
File "/usr/local/lib/python3.4/socket.py", line 507, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address

在阅读了一些与此类似的其他 stackoverflow 问题后,我使用 -d 选项运行了容器,并按如下方式访问了容器:

docker exec -ti <container name> bash

进入后,我使用 curl 和 wget 尝试访问 localhost:5000/index。 Wget 刚刚抛出 500 个错误。 Curl 返回 Werkzeug 页面(参见上面的错误转储),包括所有 html 格式。目录结构看起来不错

我知道我一定是配置不正确。我只是看不出它是什么。该应用程序在 Docker 之外工作,并且一次也不在 Docker 中。我无法从容器内部访问该页面,这表明设置有些不对,但我要么缺乏专业知识,要么没有“木有木有”的时刻。请帮助一个相对新手。

最佳答案

如果您查看错误,首先这是一个应用程序异常。这意味着您在主机上和容器内的绑定(bind)都在工作。与暴露端口无关。

如果我们再看一下异常堆栈跟踪

File "/usr/local/lib/python3.4/site-packages/couchdb/http.py", line 289, in request
conn = self.connection_pool.get(url)
File "/usr/local/lib/python3.4/site-packages/couchdb/http.py", line 507, in get
conn.connect()
File "/usr/local/lib/python3.4/http/client.py", line 871, in connect
self.timeout, self.source_address)

您正在连接到 couch DB,但该地址不正确,这就是此处引发错误的原因。错误 OSError: [Errno 99] Cannot assign requested address 有点误导,但这是因为您无法连接到可能配置错误或根本未运行的 Couch DB 服务

关于python - 无法访问 docker 容器中的 flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46656207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com