1) Define a shared network
networks:
my_shared_network:
driver: bridge
3) Make sure every service recognies the network.
services:
app1:
build: ./app1
container_name: app1
command: uvicorn server:app --reload --host=0.0.0.0 --port=8001
ports:
- 8001:8001
volumes:
- ./app1:/app
networks:
my_shared_network:
aliases:
- app1
5) Define aliases for every service. In the case mentioned above, the alias is “app1.” 6) Inside the app, make requests to the alias instead of “localhost.”
APP_2_ALIASES = "app2"
# Perform request to this url
url = "http://{}:8002/app2".format(APP_2_ALIASES)
res = requests.post(url, json={
"text": text
})
1) Make request to “host.docker.internal”
APP_3_ALIASES = "host.docker.internal"
# Perform request to the url using host.docker.internal
url = "http://{}:8003/app3".format(APP_3_ALIASES)
res = requests.post(url, json={
"text": text
})
That’s it. Refer to the code for full example.
Result..
{
"message": "Success",
"reply_app_1": "Received in app1 => Request start",
"call_result": "{\"message\":\"Success\",\"reply_app_2\":\"Received in app2 => {\\\"message\\\":\\\"Success\\\",\\\"reply_app_3\\\":\\\"Behold app3 => Request start\\\"}\"}"
}