0

I am running docker swarm with a current setup

swarm.abc.com (lets say 192.168.1.1) - master node1.abc.com - child node2.abc.com - child

I have a container running in the swarm running on port 888. If I connect to the 192.168.1.1:888 - I can access it - which is good.

I also have nginx on swarm.abc.com. in nginx config I have

server {
    listen 80;
    server_name my.domain.com;

    location / {
        proxy_pass http://localhost:888; #which should redirect me to 192.168.1.1
    }
}

However, when I connect to my.domain.com I'm getting connection timeout. Any help?

Cheers

Branislav B.
  • 125
  • 4

1 Answers1

0

Networking is namespaced in docker, and part of that namespace is the loopback device, aka 127.0.0.1 or localhost. That means localhost inside the nginx container points to just the nginx container's network, not your external host.

For container to container networking, place the containers on the same user created docker network (compose does this for you by default). Then you can connect to other containers by their container name, container id, service name, or network alias. So if your container name is app-x, and inside the container it's listening on port 999 and you have 888:999 as your port forward, then the nginx config would look like:

proxy_pass http://app-x:999;

Note that it is not necessary to publish or expose the port from app-x for nginx to reach it over container networking.

BMitch
  • 5,966
  • 1
  • 25
  • 32
  • Thanks but the nginx is not running within container but directly on the server. Would it be better to run it within container ? What would the setup be if it's not within the container ? Thanks – Branislav B. Dec 03 '19 at 10:08