【发布时间】:2019-09-15 16:56:01
【问题描述】:
我有 2 个烧瓶应用程序在不同的容器上运行。当我尝试从一个容器向另一个容器发出 API 请求时,出现以下错误:
HTTPConnectionPool(host='0.0.0.0', port=5001): Max retries exceeded with url: / (Caused by NewConnectionError(': 无法建立新连接: [Errno 111] Connection refused'))
烧瓶应用程序1:
app.route("/", methods=["GET"])
@cross_origin(supports_credentials=True)
def ads():
r=requests.get('http://0.0.0.0:5001/',verify=False)
return jsonify(r.text)
烧瓶应用程序2:
app.route("/", methods=["GET"])
@cross_origin(supports_credentials=True)
def add2():
return jsonify('Success!')
app1 的 Docker 文件:
FROM alpine:latest
RUN apk add --no-cache python3-dev \
&& pip3 install --upgrade pip
WORKDIR /
COPY . .
RUN pip3 --no-cache-dir install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python3"]
CMD ["app.py","--host", "0.0.0.0"]
app2 的 Docker 文件:
FROM alpine:latest
RUN apk add --no-cache python3-dev \
&& pip3 install --upgrade pip
WORKDIR /
COPY . .
RUN pip3 --no-cache-dir install -r requirements.txt
EXPOSE 5001
ENTRYPOINT ["python3"]
CMD ["app.py","--host", "0.0.0.0"]
使用的docker命令有:
docker run -p 5000:5000 app1
docker run -p 5001:5001 app2
如何在不使用 docker-compose 的情况下解决此错误?
【问题讨论】:
-
0.0.0.0不是有效 IP,您需要获取另一个容器的主机名才能与之通信 -
@FranciscoCouzo 我在本地机器上运行这两个容器。我也尝试了 localhost 而不是 0.0.0.0,但仍然遇到相同的错误
-
@Chetanrns,无论您是否在本地计算机上运行它们都无关紧要。您永远无法向
0.0.0.0提出请求。
标签: python docker flask python-requests linux-containers