Trying to make to different hosts of the same nginx container to communicate

Hi, I’m having some difficulties to solve this problem:

In short, I’m trying to make to different hosts of the same nginx container to communicate.

I have a container based on nginx who receives a configuration file, where I define 2 different hosts. Something like this:

host{
	listen 80;
	server_name server1.local;
	root /var/www/html/application1/;
	...
}

host{
	listen 80;
	server_name server2.local;
	root /var/www/html/application2/;
	...
}

I mount a volume from a local folder where I keep these 2 web applications, to /var/www/html/ of the container, and forward port 80 from local to container.

I changed my local etc/hosts to add these lines:

127.0.0.1	server1.local
127.0.0.1	server2.local

Also, there is a second container with php7-fpm and other packages including curl, zip, imap…
The ports are configured and nginx is able to request the processing of all php.

All works fine while accessing via browser.

Now I’m trying to communicate between them (one of the applications is an REST API) using cURL, but I cant really figure out how I do this, because the application2 inside the container doesn’t seem to be able to resolve the host server1.local.

Any thoughts?

inside the container 127.0.0.1 means INSIDE the container… NOT the host the container is running on

so from inside you are connecting to inside…

1 Like

Thanks in advance. But

  1. I changed the hosts file in my local environment… is it used inside the container?

  2. Still, since both hosts are in the same container, shouldn’t 127.0.0.1 be the right destination for the request?

from the outside , the host is the destination for both containers, on different ports. 127.0.0.1 MIGHT work for an nginx task running ON the host to talk to the two containers thru their exposed ports…

BUT, being INSIDE the container, it has its OWN unique IP address. you cannot talk to the HOST with 127.0.0.1 from inside the container, cause that means HERE local to THIS container… (just like it means HERE local to THIS host from the host…

SO, for container A to talk to container B it will have to use the IP address for container B. IP traffic will route thru the gateway, but never on the HOST network.

typically containers are not supposed to know about each other…

SO, start container B first, get its IP address (docker inspect) and pass that along to container A when you start it.

you MIGHT be able to make this easier with docker-compose and a ‘named’ network for the containers…
I have never used compose, as it didn’t exist when I started using docker, and it doesn’t fit my application requirements today.

but you would have three containers on a network. with names.
A(api client) depends on B(api server) and C(nginx) depends on both A and B

Once again, many thanks for you time, yet:

I have only 1 container with nginx with 2 hosts (virtual hosts) configured.
But I do have a second container with the php-fpm, and since its there that the curl is executed, I would say you are right on the ip resolving, assuming that the hosts file is shared.

But I still need to know, in the php container context, what is the IP’s of the nginx container, and how to access it using the right hostname since nginx will use it to determine which application will be executed…

If any more clues, I would appreciate, but once again, you have helped me allot already, so now I have something more to dig into and think about.

(Also, I already use docker-compose to lift up this system… I’ll look again for more info on that, and how to control the hosts file in each container.)

on one of my containers (ip address 172.17.0.2) I can ping the host IP address(192.168.2.33)
SO, I would add --add-host=my_host:192.168.2.33 to the docker run for the php container and use the my_host name for your curl command

i don’t know how to do that with compose, altho you might be able to pass parms on the compose up

The solution for my problem was adding the following lines to the docker-compose.yml:

 extra_hosts:
     - "<hostname_expected_by_nginx>:<ip_address_from_real_server>"

Rebuilding and re-up the docker-compose gives me the expected behaviour.

I’m still looking for a better solution because my server (development machine) has an IP address that may change with time, but for now it just works.

great info… thanks for the update…

at least in the short term you could wrap the compose up with a script that determines the ip address to use and edits the compose file…