![](/img/trans.png)
[英]Opensearch Docker Image Failed to establish a new connection: [Errno 111] Connection refused)
[英]Docker. Failed to establish a new connection: [Errno 111] Connection refused'
我想將發布請求從一個容器發送到另一個容器,兩者都是 flask 應用程序。
當我按表單中的發送按鈕時,我的請求無法發送錯誤:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=5000): Max
retries exceeded with url: /users/ (Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe0e5738ac0>: Failed to
establish a new connection: [Errno 111] Connection refused'))
我試圖在本地主機上運行它。 當我使用 docker-compose 時,一切正常,直到我嘗試發送請求。
應用程序 1 代碼(應用程序,我試圖從中發送請求):
from settings import app, db
from flask import jsonify, request
from models import User
@app.route('/users/', methods=['POST'])
def users_post():
if request.json:
new_user = User(
email=request.json['email'],
first_name=request.json['first_name'],
last_name=request.json['last_name'],
password=request.json['password'])
db.session.add(new_user)
db.session.commit()
return jsonify({'msg': 'user succesfully added'})
else:
return jsonify({'msg': 'request should be in json format'})
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
dockerfile 容器 1:
FROM python:3
COPY . ./app
WORKDIR /app
RUN pip3 install -r requirements.txt
EXPOSE 5000 5050
CMD ["python3", "app.py"]
應用程序 2 代碼:
@app.route('/', methods=['GET', 'POST'])
def users_get():
if request.method == 'POST':
request.form['email']
data = {
'email':request.form['email'],
'first_name':request.form['first_name'],
'last_name':request.form['last_name'],
'password':request.form['password']
}
r = requests.post('http://0.0.0.0:5000/users/', data=data)
print(r.text)
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5050)
應用程序 2 中的 dockerfile 與第一個類似。
docker-compose
version: '3'
services:
web:
build: ./core
command: python3 app.py
volumes:
- .:/core
ports:
- "5000:5000"
links:
- new_app
new_app:
build: ./new_app
command: python3 app.py
volumes:
- .:/new_app
ports:
- "5050:5050"
我錯過了什么?
app1 缺少端口,您應該添加它:
app.run(debug=True, host='0.0.0.0', port=5000)
從 app2 調用 app1 時,您應該使用它的主機而不是0.0.0.0
:
r = requests.post('http://web:5000/users/', data=data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.