How to make connection between two django apps in two different docker containers? How to make connection between two django apps in two different docker containers? docker docker

How to make connection between two django apps in two different docker containers?


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 psCONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMESaefa0c5d4bc6        workspace_minombre   "python manage.py ru…"   15 minutes ago      Up 15 minutes       0.0.0.0:8000->8000/tcp   minombre558e22e612f5        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.1 200 OKDate: Tue, 05 Nov 2019 20:56:01 GMTServer: WSGIServer/0.2 CPython/3.8.0Content-Type: text/html; charset=utf-8X-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.shortcuts import renderimport jsonfrom django.http import HttpResponsedef 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 $

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.shortcuts import renderimport requestsfrom django.http import HttpResponsefrom django.shortcuts import renderdef index(request):    response = requests.get('http://myapi:8001/polls/')    data = response.json()    return HttpResponse(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.1 200 OKDate: Tue, 05 Nov 2019 20:41:10 GMTServer: WSGIServer/0.2 CPython/3.8.0Content-Type: text/html; charset=utf-8X-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 OKDate: Tue, 05 Nov 2019 21:06:31 GMTServer: WSGIServer/0.2 CPython/3.8.0Content-Type: text/html; charset=utf-8X-Frame-Options: SAMEORIGINContent-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 bashroot@aefa0c5d4bc6:/code# curl -I http://127.0.0.1:8001/polls/curl: (7) Failed to connect to 127.0.0.1 port 8001: Connection refusedroot@aefa0c5d4bc6:/code#


Got it!!! I just had to mention hostname in ALLOWED_HOSTS of the api's settings.py file. Thanks for the help everyone!!!

ALLOWED_HOSTS = ['127.0.0.1',]


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: ./myapi container_name: myapi ports:   - "8001:8001"  networks:   - main command: python manage.py runserver 0.0.0.0:8001minombre: build: ./minombre container_name: minombre ports:  - "8000:8000"  networks:  - main command: python manage.py runserver 0.0.0.0:8000 depends_on:   - myapi

Now they will be part of same network so you can easily you docker DNS to hit APIs.