How To Make Connection Between Two Django Apps In Two Different Docker Containers?
Solution 1:
If both containers are deployed in same host and want to make API call from "minombre" to "myapi" Django app then you can use below URL in "minombre" view it should work.
response = requests.get('http://myapi:8001/') # where myapi is the container name
EDIT
Two Django containers minombre and myapi running on port 8000 and 8001 respectively.
(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 $
When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['localhost', '127.0.0.1', '[::1]']. [source][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.1200OKDate: Tue, 05Nov201920:56:01GMTServer: WSGIServer/0.2 CPython/3.8.0Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGINContent-Length: 11
(venv) shakeel@my-workstation 20:56:01 ~/workspace $
Now I have added my container name to allowed host
(venv) shakeel@my-workstation 21:00:42 ~/workspace $ cat myapi/myapi/settings.py |grep "ALLOWED_HOST"
ALLOWED_HOSTS = ['myapi', '127.0.0.1',]
I have added simple json response to API myapi:8001
(venv) shakeel@my-workstation 21:04:57 ~/workspace $ cat myapi/polls/views.pyfrom django.shortcutsimport render
import json
from django.httpimportHttpResponse
def index(request):
responseData = {
'id': 4,
'name': 'Test Response',
'roles' : ['Admin','User']
}
returnHttpResponse(json.dumps(responseData))
(venv) shakeel@my-workstation 21:05:02 ~/workspace $
(venv) shakeel@my-workstation 21:05:04 ~/workspace $
And now we are calling API myapi:8001 under minombre:8000's view
(venv) shakeel@my-workstation 21:05:04 ~/workspace $ cat minombre/polls/views.pyfrom django.shortcutsimport render
import requests
from django.httpimportHttpResponsefrom django.shortcutsimport render
def index(request):
response = requests.get('http://myapi:8001/polls/')
data = response.json()
returnHttpResponse(data)
(venv) shakeel@my-workstation 21:05:14 ~/workspace $
Now when you call minombre API success response.
(venv) shakeel@my-workstation 21:05:14 ~/workspace $ curl -I http://127.0.0.1:8000/polls/
HTTP/1.1200OKDate: Tue, 05Nov201920:41:10GMTServer: WSGIServer/0.2 CPython/3.8.0Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGINContent-Length: 11
(venv) shakeel@my-workstation 21:05:14 ~/workspace $
But with settings ALLOWED_HOSTS = ['myapi', '127.0.0.1',]
we cannot access from container, however you can still connect from host machine.
(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#
Solution 2:
Got it!!! I just had to mention hostname in ALLOWED_HOSTS of the api'ssettings.py file. Thanks for the help everyone!!!
ALLOWED_HOSTS = ['127.0.0.1',]
Solution 3:
Here is solution you just need two apps to make part of the same network.
Below is a docker-compose.yml file.
version:'3'networks:main:services:myapi:build:./myapicontainer_name:myapiports:-"8001:8001"networks:-maincommand:pythonmanage.pyrunserver0.0.0.0:8001minombre:build:./minombrecontainer_name:minombreports:-"8000:8000"networks:-maincommand:pythonmanage.pyrunserver0.0.0.0:8000depends_on:-myapi
Now they will be part of same network so you can easily you docker DNS to hit APIs.
Post a Comment for "How To Make Connection Between Two Django Apps In Two Different Docker Containers?"