我有一个python flask web应用程序,它使用了一个正在开发中的python库,我想对接它。这个图书馆发送SPARQL查询来搜索可用的书籍,从Gutenberg项目中获得的书籍集合。这些数据以RDF格式存储在Fuseki服务器中。Fuseki服务器通过docker单独运行,查询的SPARQL端点如下:http://localhost:3030/gutenberg/sparql
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>
当我环顾四周时,我发现以下question有同样的错误,即socket.error:[errno 99] cannot assign requested address
在docker容器中运行flask应用程序。已解决将flask IP地址设置为
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
并在Docker文件上配置端口,这是我所做的(回想一下Docker文件EXPOSE 80
的第22行)。
我的猜测是,我运行应用程序的docker容器不能被其他设备访问,也就是Fuseki服务器,但我找不到原因,我已经用尽了找出答案的方法。
发布于 2021-05-28 17:20:16
我找到了这个问题的答案。正如@Christopher所说,问题在于容器试图从内部访问我在本地主机上的Fuseki服务器:3000,而它位于主机上。为了解决这个问题,我在docker run命令--net="host"
中添加了以下参数,并删除了-p 80:80
。完整的docker run命令如下所示
docker run --rm --net="host" wedh-dhtk-test
如果我理解正确的话,它允许我的docker容器指向服务器所在的docker主机。无论如何,它解决了我的问题,因为容器化的应用程序现在可以像预期的那样与Fuseki服务器通信。
https://stackoverflow.com/questions/67723428
复制相似问题