Nor*_*dle 5 python flask docker
在 2 个独立的容器内有 2 个 Flask 应用程序,我认为可以GET从一个容器运行请求并从第二个容器返回信息,但我得到了[Errno 111] Connection refused。通过以下设置:
应用程序1.py
@app.route('/get_message')
def get_message():
message = requests.get('http://web2:5001/return_message')
return message.text
if __name__ == '__main__':
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
应用程序2.py
@app.route('/return_message')
def return_message():
return 'the message'
if __name__ == '__main__':
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml
version: "3.7"
services:
web1:
build: ./web1
container_name: web1
ports:
- "5000:5000"
web2:
build: ./web2
container_name: web2
ports:
- "5001:5001"
Run Code Online (Sandbox Code Playgroud)
app1 的端点在访问http://127.0.0.1:5000/get_message时有效,但 Flask 返回:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='web2', port=5001): Max retries exceeded with url: /return_message (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb866642d30>: Failed to establish a new connection: [Errno 111] Connection refused',))
Run Code Online (Sandbox Code Playgroud)
尝试为 web2 容器分配静态 IP 地址并在get请求中使用该 IP 不起作用,在文件内使用networks,link或 也不起作用。还尝试以不同的组合公开两个容器上的不同端口,但没有收到它们之间的消息。depends_ondocker-compose
我认为当你旋转你的应用程序时,它们都在端口上运行5000,但在不同的容器中,所以尝试将你的更改app1.py为:
@app.route('/get_message')
def get_message():
message = requests.get('http://web2:5000/return_message')
return message.text
if __name__ == '__main__':
app.run(host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
并且还更改 docker-compose 中的端口web2:
version: "3.7"
services:
web1:
build: ./web1
container_name: web1
ports:
- "5000:5000"
web2:
build: ./web2
container_name: web2
ports:
- "5001:5000"
Run Code Online (Sandbox Code Playgroud)
但是,如果您不想web2从主机访问,则无需更改 docker-compose 中的端口 - 请记住,这里我将主机的端口 5001 映射到 web2 容器的端口 5000,并且两个容器都在端口 5000 上公开应用程序。
请记住,这些是单独的容器,就像单独的环境一样。另请记住,这EXPOSE仅用于记录您在此端口公开某些服务 - 它不会使容器中的应用程序在此端口上运行。
| 归档时间: |
|
| 查看次数: |
3247 次 |
| 最近记录: |