我创建了两个应用程序``myapi''和``minombre'',其中``minombre''将向``myapi''发出一个简单的GET请求,并将它们放入两个单独的Docker容器中。 api不传递数据。发出GET请求的“minombre”的views.py如下:
def index(request):
response = requests.get('http://myapi')
print(response)
data = response.json()
name = data['user']
message = data['message']
return HttpResponse('<h2> {} </h2> <br> <h5> {} </h5>'.format(name, message))
这是我用来使容器运行的docker-compose.yml。version: '3'
services:
myapi:
build: ./myapi
container_name: myapi
ports:
- "8001:8001"
command: python manage.py runserver 0.0.0.0:8001
minombre:
build: ./minombre
container_name: minombre
ports:
- "8000:8000"
command: python manage.py runserver 0.0.0.0:8000
depends_on:
- myapi
这是异常(exception):Exception Type: ConnectionError
Exception Value:
HTTPConnectionPool(host='myapi', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f69b902bbd0>: Failed to establish a new connection: [Errno 111] Connection refused'))
最佳答案
如果两个容器都部署在同一主机上,并且希望从“minombre”到“myapi” Django应用进行API调用,则可以在“minombre” View 中使用以下URL,它应该可以工作。
response = requests.get('http://myapi:8001/') # where myapi is the container name
编辑
两个Django容器minombre和myapi分别在端口8000和8001上运行。
(venv) shakeel@my-workstation 20:54:44 ~/workspace $ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aefa0c5d4bc6 workspace_minombre "python manage.py ru…" 15 minutes ago Up 15 minutes 0.0.0.0:8000->8000/tcp minombre
558e22e612f5 workspace_myapi "python manage.py ru…" 15 minutes ago Up 15 minutes 0.0.0.0:8001->8001/tcp myapi
(venv) shakeel@my-workstation 20:54:48 ~/workspace $
当DEBUG为True且ALLOWED_HOSTS为空时,将针对['localhost','127.0.0.1','[:: 1]']验证主机。 [来源] [1]
(venv) shakeel@my-workstation 20:54:51 ~/workspace $ cat minombre/minombre/settings.py | grep "ALLOWED_HOST"
ALLOWED_HOSTS = []
(venv) shakeel@my-workstation 20:55:29 ~/workspace $ curl -I http://127.0.0.1:8000/polls/
HTTP/1.1 200 OK
Date: Tue, 05 Nov 2019 20:56:01 GMT
Server: WSGIServer/0.2 CPython/3.8.0
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 11
(venv) shakeel@my-workstation 20:56:01 ~/workspace $
现在,我已将容器名称添加到允许的主机
(venv) shakeel@my-workstation 21:00:42 ~/workspace $ cat myapi/myapi/settings.py |grep "ALLOWED_HOST"
ALLOWED_HOSTS = ['myapi', '127.0.0.1',]
我向API myapi:8001添加了简单的json响应
(venv) shakeel@my-workstation 21:04:57 ~/workspace $ cat myapi/polls/views.py
from django.shortcuts import render
import json
from django.http import HttpResponse
def index(request):
responseData = {
'id': 4,
'name': 'Test Response',
'roles' : ['Admin','User']
}
return HttpResponse(json.dumps(responseData))
(venv) shakeel@my-workstation 21:05:02 ~/workspace $
(venv) shakeel@my-workstation 21:05:04 ~/workspace $
现在我们在minombre:8000的 View 下调用API myapi:8001
(venv) shakeel@my-workstation 21:05:04 ~/workspace $ cat minombre/polls/views.py
from django.shortcuts import render
import requests
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
response = requests.get('http://myapi:8001/polls/')
data = response.json()
return HttpResponse(data)
(venv) shakeel@my-workstation 21:05:14 ~/workspace $
现在,当您调用minombre API成功响应时。
(venv) shakeel@my-workstation 21:05:14 ~/workspace $ curl -I http://127.0.0.1:8000/polls/
HTTP/1.1 200 OK
Date: Tue, 05 Nov 2019 20:41:10 GMT
Server: WSGIServer/0.2 CPython/3.8.0
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 11
(venv) shakeel@my-workstation 21:05:14 ~/workspace $
但是使用
ALLOWED_HOSTS = ['myapi', '127.0.0.1',]
设置,我们无法从容器访问,但是您仍然可以从主机连接。(venv) shakeel@my-workstation 21:06:19 ~/workspace $ curl -I http://127.0.0.1:8001/polls/
HTTP/1.1 200 OK
Date: Tue, 05 Nov 2019 21:06:31 GMT
Server: WSGIServer/0.2 CPython/3.8.0
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 62
(venv) shakeel@my-workstation 21:07:15 ~/workspace $
(venv) shakeel@my-workstation 21:08:15 ~/workspace $ cat myapi/myapi/settings.py |grep "ALLOWED_HOST"
ALLOWED_HOSTS = ['myapi', '127.0.0.1',]
(venv) shakeel@my-workstation 21:08:35 ~/workspace $ sudo docker exec -it minombre bash
root@aefa0c5d4bc6:/code# curl -I http://127.0.0.1:8001/polls/
curl: (7) Failed to connect to 127.0.0.1 port 8001: Connection refused
root@aefa0c5d4bc6:/code#