Docker Community Forums

Share and learn in the Docker community.

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

(Palmeida4gw) #1

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:

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

	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:	server1.local	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?

(Sam) #2

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

so from inside you are connecting to inside…

(Palmeida4gw) #3

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 be the right destination for the request?

(Sam) #4

from the outside , the host is the destination for both containers, on different ports. 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 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

(Palmeida4gw) #5

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.)

(Sam) #6

on one of my containers (ip address I can ping the host IP address(
SO, I would add --add-host=my_host: 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

(Palmeida4gw) #7

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

     - "<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.

(Sam) #8

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…