python - OSError:[Errno 99] 无法分配请求的地址:flask、python 和 docker
问题描述
我有一个 python flask web 应用程序,它使用一个正在开发中的 python 库,我想 dockerize。这个图书馆发送 SPARQL 查询来搜索可用的书籍,从古腾堡项目获得的书籍收藏。此数据以 RDF 格式存储在 Fuseki 服务器中。Fuseki 服务器通过 docker 单独运行,查询的 SPARQL 端点如下:http://localhost:3030/gutenberg/sparql
使用以下 Docker 文件对 Web 应用进行 docker 化
FROM python:3.8-buster
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt
COPY . /app
CMD celery -A webapp.app.celery worker --loglevel=DEBUG
CMD export FLASK_APP=wsgi.py
EXPOSE 80
ENTRYPOINT [ "python" ]
CMD [ "webapp/app.py" ]
使用以下命令:docker build -t wedh-dhtk-test .
构建 docker 映像后,我使用以下命令运行它:docker run --rm -p 80:80 wedh-dhtk-test
我遇到的问题是运行 Web 应用程序的 docker 容器似乎无法连接到 SPARQL 端点,并向我抛出以下两条错误消息:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/urllib/request.py", line 1354, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/usr/local/lib/python3.8/http/client.py", line 1252, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/lib/python3.8/http/client.py", line 1298, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.8/http/client.py", line 1247, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.8/http/client.py", line 1007, in _send_output
self.send(msg)
File "/usr/local/lib/python3.8/http/client.py", line 947, in send
self.connect()
File "/usr/local/lib/python3.8/http/client.py", line 918, in connect
self.sock = self._create_connection(
File "/usr/local/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/local/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "webapp/app.py", line 16, in <module>
AUTHORS = gd.get(what="author", name="all")
File "/usr/local/lib/python3.8/site-packages/dhtk/extensions/gutenberg/__init__.py", line 98, in get
response = self.wrapper.all_authors()
File "/usr/local/lib/python3.8/site-packages/dhtk/extensions/gutenberg/api/data.py", line 387, in all_authors
return [result["author"] for result in self._get_query_results(query)]
File "/usr/local/lib/python3.8/site-packages/dhtk/extensions/gutenberg/api/data.py", line 789, in _get_query_results
query_results = sparql.queryAndConvert()
File "/usr/local/lib/python3.8/site-packages/SPARQLWrapper/Wrapper.py", line 1114, in queryAndConvert
res = self.query()
File "/usr/local/lib/python3.8/site-packages/SPARQLWrapper/Wrapper.py", line 1107, in query
return QueryResult(self._query())
File "/usr/local/lib/python3.8/site-packages/SPARQLWrapper/Wrapper.py", line 1073, in _query
response = urlopener(request)
File "/usr/local/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.8/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/usr/local/lib/python3.8/urllib/request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/usr/local/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/usr/local/lib/python3.8/urllib/request.py", line 1383, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/local/lib/python3.8/urllib/request.py", line 1357, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 99] Cannot assign requested address>
环顾四周,我发现以下问题有同样的错误,即socket.error:[errno 99] cannot assign requested address
在 docker 容器中运行烧瓶应用程序。问题已解决,将烧瓶 IP 地址设置为
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
并在 Docker 文件上配置一个端口,我这样做了(回忆一下 Docker 文件的第 22 行EXPOSE 80
)。
我的猜测是,其他设备(即 Fuseki 服务器)无法访问我运行我的应用程序的 docker 容器,但我找不到原因,并且我已经用尽了找出的方法。
解决方案
我找到了这个问题的答案。正如@Christopher 所说,问题在于容器试图从内部访问我在 localhost:3000 上的 Fuseki 服务器,而它位于主机上。为了解决这个问题,我在 docker run 命令中添加了以下参数--net="host"
并删除了-p 80:80
. 完整的 docker run 命令如下所示
docker run --rm --net="host" wedh-dhtk-test
如果我理解正确,它允许我的 docker 容器指向服务器所在的 docker 主机。无论如何,它解决了我的问题,因为容器化应用程序现在正在按预期与 Fuseki 服务器通信。
推荐阅读
- javascript - 类元素的点击显示不起作用
- java - Admob 横幅广告不显示
- mysql - 是否可以在 Laravel 中加入如下表格?
- python - 图像处理:鱼测量的问题
- python-3.x - 如何在某个进程结束时在 python 中启用 qt5 按钮
- ios - isHidden 或 view.removeFromSuperView 需要很长时间
- python - 以 numpy 的风格重构矩阵排列
- java - RXJava 订阅不可转换类型;不能将 'void' 转换为 'org.reactivestreams.Subscription?
- html - Bootstrap 3 复选框 - 在复选框和文本之间添加空格
- android - 如何将回调函数从 Activity 中的一个片段传递给第二个 Activity?