我一直在尝试在docker容器中创建一个烧瓶应用程序。该应用程序在通过Flask开发服务器运行时非常有效,包括从网络上的其他计算机访问网页。
我一直在尝试为应用构建一个Docker容器。问题是我无法访问任何网页。我得到500个错误。
我的一些设置:
#!flask_v1/bin/python3
from app import app
app.run(debug=True, host='0.0.0.0')
其中flask_v1是virtualenv目录。
FROM python:3.4
COPY . /site
workdir /site
RUN pip install -r requirements.txt
ENV NAME sitename
CMD ["python", "./run.py"]
我从此版本中删除了 EXPOSE 5000 ,但是它出现了同样的问题。它最初被置于点线下。
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
然后我运行build命令,如下所示(dockername替换为实际名称):
docker build -t dockername .
构建完成,我按如下方式运行图像:
docker run -p 5000:5000 dockername
尝试连接到localhost:5000 / index(应用程序的'home'),或者只是localhost:5000,或127.0.0.1:5000/index,或[host_ip]:5000 / index,或者[container_ip]:5000 / index all产生了同样的错误:
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中不会运行一次。我无法从容器内部访问页面告诉我一些关于设置的事情是不对的,但是我要么缺乏知道如何,要么拥有“木树林”时刻。请帮助一位亲戚。
答案 0 :(得分:2)
如果查看错误,首先是应用程序异常。这意味着您在主机和容器内的绑定都可以正常工作。它与暴露的端口无关。
如果我们再看一下异常堆栈跟踪
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)
您正在连接到沙发数据库,并且该地址不正确,这就是抛出错误的原因。错误OSError: [Errno 99] Cannot assign requested address
有点误导,但这是因为您无法连接到可能配置错误或根本无法运行的Couch DB服务