无法到达Docker容器中的Flask应用 [英] Cannot reach flask app in docker container

查看:125
本文介绍了无法到达Docker容器中的Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在docker容器内创建一个flask应用程序。当通过Flask开发服务器运行该应用程序时,它可以完美运行,包括从网络上其他计算机访问网页。

I have been trying to create a flask app inside a docker container. The app works perfectly when run through the Flask development server, including access to the web pages from other machines on the network.

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

I have been trying to build a Docker container for the app. The problem is that I cannot access any web pages. I get 500 errors.

一些设置:

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

其中flask_v1是virtualenv目录。

Where flask_v1 is the virtualenv directory.

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

我从此版本中删除了 EXPOSE 5000 ,但存在相同的问题。

I removed EXPOSE 5000 from this version, but the same issues occurred with it present. It was originally placed under the pip line.

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替换为实际名称):

Then I ran the build command as follows (with dockername replaced by an actual name):

docker build -t dockername .

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

Build completes and I run the image as follows:

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都产生相同的错误:

Attempting to connect to the app at localhost:5000/index (the 'home' for the app), or just localhost:5000, or 127.0.0.1:5000/index, or [host_ip]:5000/index, or [container_ip]:5000/index all produced the same error:

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选项运行了容器,并按如下方式访问了该容器:

After reading some other stackoverflow issues similar to this, I ran the container with the -d option and I accessed the container as follows:

docker exec -ti <container name> bash

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

Once inside, using curl and wget I attempted to access localhost:5000/index. Wget just threw 500 errors. Curl returns the Werkzeug page (see error dump above), including all of the html formatting. The directory structure looks fine

我知道我必须配置不正确。我只是看不到它是什么。该应用程序可在Docker外部运行,而不会一次在Docker中运行。我无法从容器内部访问该页面,这告诉我有关设置的某些信息不正确,但是我要么不知道该怎么做,要么缺少树木换木的知识。时刻。请帮助一个相对新手。

I know I must have something configured incorrectly. I just can't see what it is. The app works outside Docker and doesn't once in Docker. That I can't access the page from inside the container tells me something isn't right about the setup, but I'm either lacking the know how or having a "wood for the trees" moment. Please help a relative newbie.

推荐答案

如果您看到此错误,则首先是应用程序异常。这意味着您对主机和容器内部的绑定都可以正常工作。

If you look at the error, first of all this is an application exception. This means your binding on host and inside the container are all working. And it has nothing to with exposed port.

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

If we have a second look at the exception stack trace

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]无法分配请求的地址有点误导,但这是因为您无法连接到Couch DB服务,该服务可能配置错误或根本没有运行

You are connecting to couch DB and that address is not correct and that is what is throwing the error here. The error OSError: [Errno 99] Cannot assign requested address is a bit misleading, but it is because you are unable to connect to the Couch DB service which may be configured wrongly or not running at all

这篇关于无法到达Docker容器中的Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆