Consume a docker container inside Django docker container? Connecting two docker containers

Question:

I have a Django container and I want to consume another DL container inside it? For example, I have a Django app that predicting images classes and I want to make the prediction using a docker container and not a python library. That Django app will be containerised as well. In production, I will have three docker containers: Django container + Postgres container + YoloV5 container. How can I link the Django with the YoloV5 so that the prediction inside the Django will be done using the YoloV5?

I want to connect a deep learning container with Django container to make prediction using the DL container and not a python package.

Asked By: Joker King

||

Answers:

The easiest way to do this is to make a network call to the other container. You may find it simplest to wrap the YoloV5 code in a very thin web layer, e.g. using Flask, to create an API. Then call that in your Django container when you need it using requests.

Answered By: Nick K9

As suggested by Nick and others, the solution is: by calling the YoloV5 docker container inside Django container using host.docker.internal. I mean that inside Django container (views.py) I used host.docker.internal to call the YoloV5 container.
Update on 29th March 2023:
Instead of using the host.docker.internal. As suggested by Nick we can use Requests:
An example, say you have two containers A and B "Make sure that both of them are connected together". Container A has a function to add two numbers and I want to use this function in Container B. Then Inside Container B:

import requests
data = {
    'x': 5,
    'y': 3
}
response = requests.post('http://containerA:8000/add', json=data)
result = response.json()
print(result) # outputs {'result': 8}

Note that ContainerA is running on port 8000.

Answered By: Joker King