两个单独的Flask应用在Docker容器中进行通信

时间:2018-12-01 06:15:25

标签: python rest api docker flask

编辑:

我设法对两个容器使用network_mode: "host"使它正常工作,如下所示。现在很好。但是,我觉得这是一个不好的hack,我想更多地了解我对容器所做的错误,而不是对其进行破解。

让我的两个Flask应用程序在两个单独的Docker容器中通信时遇到问题。我已经从m1在docker容器外部运行了app.py,并且可以正常工作,但是显然需要做一些不同的事情才能在docker容器外部进行通信。

我得到此追溯:

Traceback (most recent call last):
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
m1_1  |     return self.wsgi_app(environ, start_response)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
m1_1  |     response = self.handle_exception(e)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
m1_1  |     reraise(exc_type, exc_value, tb)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
m1_1  |     raise value
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
m1_1  |     response = self.full_dispatch_request()
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
m1_1  |     rv = self.handle_user_exception(e)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
m1_1  |     reraise(exc_type, exc_value, tb)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
m1_1  |     raise value
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
m1_1  |     rv = self.dispatch_request()
m1_1  |   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
m1_1  |     return self.view_functions[rule.endpoint](**req.view_args)
m1_1  |   File "/app/app.py", line 11, in home
m1_1  |     res = requests.get(url)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 75, in get
m1_1  |     return request('get', url, params=params, **kwargs)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 60, in request
m1_1  |     return session.request(method=method, url=url, **kwargs)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
m1_1  |     resp = self.send(prep, **send_kwargs)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
m1_1  |     r = adapter.send(request, **kwargs)
m1_1  |   File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
m1_1  |     raise ConnectionError(e, request=request)
m1_1  | requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8002): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ff58dd1bd30>: Failed to establish a new connection: [Errno 111] Connection refused',))

来自m1的app.py

from flask import Flask, jsonify
import requests
import json

app = Flask(__name__)


@app.route("/")
def home():
    url = "http://127.0.0.1:8002/"
    res = requests.get(url)
    dictFromServer = res.json()
    return dictFromServer['message']


if __name__=='__main__':
    app.run(debug=True, host='0.0.0.0')

app.py来自m2

from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/')
def hello_world():
    return jsonify({ 'message': 'Hello From M2'})


if __name__=='__main__':
    app.run(debug=True, host='0.0.0.0')

dockerfile

FROM python:3.6-alpine
WORKDIR /app
COPY requirements.txt /app
COPY app.py /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]

docker-compose

version: '3'

services:
      m1:
        build: m1
        ports:
          - "8001:5000"
      m2:
        build: m2
        ports:
          - "8002:5000"

1 个答案:

答案 0 :(得分:0)

Docker容器的IP地址不同,因此m1中的127.0.0.1仅解析为m1容器。

两个修复程序:

    两个容器的
  1. “网络:主机”将使用主机网络堆栈,因此m1和m2将具有相同的IP地址。这样,“ 127.0.0.1”就可以使用。

  2. 将“ 127.0.0.1”替换为“ m2”。这将使用内置的DNS服务器,并为m2容器解析正确的IP地址。这种方法的另一个好处是,两个容器可以使用相同的端口,因为它们具有不同的地址。