I'm trying to make a request from a container to another container on the same host machine. It turns out, the request is declined and I am unable to get the information I would like. To illustrate, I made a small diagram:
On "machine 1" I have 2 services running on docker-compose: App1 has 2 services and needs to make the request for App2. App2 is the service that will receive the request, has only 1 service and listens on port 4202.
docker-compose.yaml for app1
version: '3.9'
services:
main_app:
build: .
depends_on:
- db_app
db_app:
restart: always
image: redis
docker-compose.yaml for app2
version: '3.9'
services:
main_app:
build: .
ports:
- "4202:5000"
Connection doesn't work. Which is very strange, as other connections work, such as:
In this image, on another machine machine 2 I use curl to try to reach the container that is running inside machine 1. On port 4202, I make a request for a domain name.domain.example that is on the internal network and it is resolved by internal DNS and after that, it does successfully requested the container on machine 1.
In this other case, I create a container inside machine 2 and use curl to make a request to name.domain.example and the result is the same: success. I can reach the container inside the machine 1.
Here, I use curl on machine 1 (the same machine that is running the service that will be requested). The request is made to name.domain.example and the service is successfully reached.
In this case, this is where the request fails. This time, I try to make the request from within the app1 container, the request is made to name.domain.example and the DNS is able to resolve it, but when the request is made, it ends up being refused.
Inside app1, the request I try to make is through python, from the requests library:
import requests
r = requests.get("name.domain.example:4202")
# But, Exception...:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='name.domain.example', port=4202): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f74fbcb1100>: Failed to establish a new connection: [Errno 111] Connection refused'))
I tried to put app1 on the network "host" --network=host or on the docker-composenetwork_mode="host"and I can reach the container of app2. However, this is not useful for my case since I have other services from the compose stack that I need, in addition to the fact that as the services increase and depending more on each other, this ends up becoming a spider web.




