Docker Community Forums

Share and learn in the Docker community.

Accessing a hostname mapped to inside container


I have two containers, A + B

A is a container which holds a php website, inside this container, I am trying to execute a guzzlehttp request to the php api running on container B, container A is the frontend of a website

B is a container which holds a php api, its the backend of the website

On my macbook, I have various hostnames in /etc/hosts which allow me to hit a specific url and when that request hits the php code, the nginx configuration passes the request to the code which can use the hostname to determine the country code, for example, or the request and route to the correct controller.

So in my hosts file, I have something like this:

So when I open one of these in my browser or postman, I can see they are hitting the correct container and the correct code is running. This is all working without a glitch.

The problem starts when the php website attempts to communicate with the php api, it apparently cannot do it. To make this simpler to explain, I can open a shell into the php website container and use curl, cause this will be a lot easier to explain to you, so I execute the command

curl -v

and it outputs

* Rebuilt URL to:
* Hostname was NOT found in DNS cache
*   Trying
* connect to port 80 failed: Connection refused
* Failed to connect to port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to port 80: Connection refused

So externally on my macbook, I can open this endpoint and it works without any problem, but inside a container, for some reason, I cannot access this endpoint, using the hostname anymore.

However, I wanted to know whether the php api would respond at all, even with the ip address, to see whether its a general connection issue, but it can, however because the ip address is not a hostname, the php code cannot figure out what it should run and it crashes with output I know and expect, so the code runs when trying to access the container through its ip address.

But if I use the hostname, then bad luck, it won’t work, does anybody know why and if so, what a potential solution is?

Did you try to link this domain to container A?