gpt4 book ai didi

python - 如何在两个不同的 docker 容器中的两个 django 应用程序之间建立连接?

转载 作者:行者123 更新时间:2023-12-02 18:00:41 28 4
gpt4 key购买 nike

我创建了两个应用程序“myapi”和“minombre”,其中“minombre”将向“myapi”发出一个简单的 GET 请求,并将它们放入两个单独的 docker 容器中。运行“docker-compose up”后,容器运行,但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#

关于python - 如何在两个不同的 docker 容器中的两个 django 应用程序之间建立连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58705959/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com