Docker name resolution

Asked

Viewed 118 times

0

I am setting up a reverse proxy with Nginx + Docker.

I have 02 machines that are in the bridge network.

proxy : 172.17.0.2 app : 172.17.0.3

Both containers are configured with name

 server {
       listen 80;
       server_name portainer.domain;
       location / {
           proxy_pass http://localhost:9000;
           proxy_redirect off;
           proxy_bind 127.0.0.1;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Host $server_name;
          client_max_body_size 0;
      }

}

With this setting when accessing http://portainer.domain I get error 502 - Bad Gateway.

When I switch to IP forwarding as below works perfectly.

 server {
       listen 80;
       server_name portainer.domain;
       location / {
           proxy_pass http://172.17.0.3:9000;
           proxy_redirect off;
           proxy_bind 127.0.0.1;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Host $server_name;
          client_max_body_size 0;
      }

Both containers are in the default bridge network.

If I connect in the console of both containers I can drip by IP, but not by container name.

On host machine I also cannot drip by container name, only by IP.

I’ve tried to put too proxy_pass http://portainer:9000;, where portainer is the container name I wish to access and did not succeed.

The client machine is accessing the reverse proxy server normally, the problem is time the proxy server forwards the request internally.

1 answer

1


1) You probably only have containers, in one machine.

2) Making any tethering by container IP is not sound for Docker infrastructure.

3) Each container has its own IP, different from the host IP. Therefore for each of them localhost is itself and not the host.

4) Container IP varies over every recreation and possibly even in Restart.

5) In a bridge custom network (which means that it is not the default) the resolution of names happens by the container name, service name (in the case of swarm) or given alias. Use this information to correctly address your target container.

6) Spend a little time studying Ocker, end-to-end. Your mistake is a mere problem of understanding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.