0

I have two docker containers. One runs Kestrel (172.17.0.3), The other runs Nginx (172.17.0.4) using a reverse proxy to connect to Kestrel. Nginx connects fine when I use internal Docker ip of Kestrel container but when I try to connect to Kestrel using container's hostname in nginx.conf (kestral) I get following error:

2020/06/30 00:23:03 [emerg] 58#58: host not found in upstream "kestrel" in /etc/nginx/nginx.conf:7 nginx: [emerg] host not found in upstream "kestrel" in /etc/nginx/nginx.conf:7

I launched containers with these two lines

docker run -d --name kestrel --restart always -h kestrel mykestrelimage
docker run -d --name nginx --restart always -p 80:80 -h nginx mynginximage

My nginx.conf file below.

http {
        # I've tried with and without line below that I found on Stackoverflow
        resolver 127.0.0.11 ipv6=off;
        server {
                listen 80;
                location / {
                        # lines below don't work 
                        # proxy_pass http//kestrel:80;
                        # proxy_pass http//kestrel
                        # proxy_pass http//kestrel:80/;
                        # proxy_pass http//kestrel/;


                        # when I put internal docker ip of Kestrel server works fine  
                        proxy_pass http://172.17.0.3:80/;
                }
        }
}
events {

}
Anon Ymous
  • 139
  • 1
  • 2
  • 10
  • Most likely they are on different docker networks as you didn't state if you put them on the same docker network. – Shawn C. Jun 30 '20 at 02:53
  • Isn't Docker default to put everything on same internal network? (Both seem to be on 172.17.0.x ) – Anon Ymous Jun 30 '20 at 12:16
  • [docker network connect](https://docs.docker.com/engine/reference/commandline/network_connect/) states "Once connected, the container can communicate with other containers in the same network." – Shawn C. Jun 30 '20 at 13:42
  • The kestral container is only on docker's internal network. The nginx container is both on docker internal and public network. I tried setting dns server to 127.0.0.11 in nginx.conf just in case that was reason it couldn't resolve hostname but it didn't seem to have any effect. (I can ping ip of kestral fine from inside nginx container but can't ping hostname) – Anon Ymous Jun 30 '20 at 14:42
  • Per the docs "Once connected in network, containers can communicate using only another container’s IP address or name. " – Shawn C. Jun 30 '20 at 15:02
  • Did you try to use /etc/hosts to resolve 172.17.0.3 kestrel – Yoandry Collazo Jun 30 '20 at 15:12
  • With current setup containers can communicate using internal ip. What they can't do is communicate using name (either container name or hostname which I made the same). When I try to ping from nginx > kestral or from kestral > nginx using hostname it doesn't reolve. – Anon Ymous Jun 30 '20 at 15:20
  • Can you elabourate what you mean by "use /etc/hosts to resolve"? – Anon Ymous Jun 30 '20 at 15:22

1 Answers1

-2

I figured out solution to my problem. There were two issues.

First problem: By default Docker uses default bridge network when creating containers. The default Docker bridge network does not resolve DNS though. You have to create a custom bridge network and then specify network when creating docker containers. The below allowed me to ping between containers using hostname

docker network create --driver=bridge mycustomnetwork

docker run -d --name=kestrel --restart=always -h kestrel.local --network=mycustomnetwork mykestrelimage

docker run -d --name=nginx --restart always  -p 80:80 -h nginx.local --network=mycustomnetwork mynginximage

Second problem: Even though it was only one kestrel server for some reason Nginx required that I setup an upstream section in /etc/nginx/nginx.conf

http {
        upstream backendservers {
                server kestrel;
        }
        server {
                listen 80;
                location / {
                        proxy_pass http://backendservers/;
                }
        }
}

events {

}
Anon Ymous
  • 139
  • 1
  • 2
  • 10