【发布时间】:2021-05-28 17:20:15
【问题描述】:
我有一个 python 烧瓶网络应用程序,它使用了一个正在开发中的我想要 dockerize 的 python 库。这个库发送 SPARQL 查询来搜索可用的书籍,从 Gutenberg 项目获得的书籍集合。此数据以 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>
环顾四周,我发现以下question 有同样的错误,即socket.error:[errno 99] cannot assign requested address 在docker 容器中运行flask 应用程序。问题已解决,将烧瓶 IP 地址设置为
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
并在 Docker 文件上配置一个端口,我这样做了(回忆一下 Docker 文件的第 22 行 EXPOSE 80)。
我的猜测是其他设备(即 Fuseki 服务器)无法访问我运行我的应用程序的 docker 容器,但我没有找到原因,并且已经用尽了找出的方法。
【问题讨论】:
-
我想知道this 是否会有所帮助。看起来问题是您正在尝试从 docker 内部访问 localhost:3000 - 它认为 api 也在您的 docker 文件中。您需要访问主机上的 api。我认为该链接说明了如何创建连接。