Search code examples
dockerdocker-swarm

Docker swarm and service discovery


I'm moving away from docker-compose files to using docker swarm but I just can't figure this out.

I have two services - a nginx proxy, and a website both running just fine in docker swarm (which has three nodes)

The issue I've got is I need to configure nginx to proxy_pass to the backend website. Currently the only way I can get this to work is by specifying an ip address of one of the nodes.

My services are created as follows:

docker service create --mount type=bind,source=/../nginx.conf,target=/etc/nginx/conf.d/default.conf \ -p 443:443 \ --name nginx \ nginx

and

docker service create --name ynab \ -p 5000:5000 \ --replicas 2 \ scottrobertson/fintech-to-ynab

I've tried using the service name but that just doesn't work.

Really I don't think I should have to even expose the ynab service ports (at least that would work when I used docker-compose)

In one of the nginx containers I have tried the following: root@5fabc5611264:/# curl http://ynab:5000/ping curl: (6) Could not resolve host: ynab root@5fabc5611264:/# curl http://nginx:5000/ping curl: (6) Could not resolve host: nginx root@5fabc5611264:/# curl http://127.0.0.1:5000/ping curl: (7) Failed to connect to 127.0.0.1 port 5000: Connection refused root@5fabc5611264:/# curl http://localhost:5000/ping curl: (7) Failed to connect to localhost port 5000: Connection refused

Using the process list I tried connecting to the running instances id, and name: root@5fabc5611264:/# curl http://ynab.1:5000/ping curl: (6) Could not resolve host: ynab.1 root@5fabc5611264:/# curl http://pj0ekc6i7n0v:5000/ping curl: (6) Could not resolve host: pj0ekc6i7n0v

But I can only get it to work if I use the nodes public ip addresses: root@5fabc5611264:/# curl http://192.168.1.52:5000/ping pong root@5fabc5611264:/# curl http://192.168.1.53:5000/ping pong root@5fabc5611264:/# curl http://192.168.1.51:5000/ping pong

I really don't want to use a public ip in case that node goes down. I'm sure I must just be doing something wrong!


Solution

  • The services need to be connected to the same network for this to work.

    $ docker network create -d overlay fake-internet-points
    $ docker service create --name service_one --network fake-internet-points [...]
    $ docker service create --name service_two --network fake-internet-points [...]