0

I have a plumber service and use dockercloud/haproxy for load balancing. I use docker-compose for running my Docker applications:

docker-compose.yml

version: '3'
services:
  plumber:
    image: registry.code.roche.com/tmbd/tsi-api:latest
    command: /plumber.R
    volumes:
     - ./plumber/plumber.R:/plumber.R
    restart: always

  lb:
    image: 'dockercloud/haproxy:latest'
    volumes:
     - /var/run/docker.sock:/var/run/docker.sock
    restart: always
    links:
     - plumber
    ports:
     - 80:80

It works fine with this configuration. Now I have extended my plumber.R script to communicate with a MongoDB on a remote server to which I established a SSH tunnel with

autossh -M 20000 -L 9999:localhost:27017 <user>@<mongodb-server> -vN -p22

In addition I added the network_mode: host to the plumber service config in the yml file to make it work.

However, according to here network_mode: host cannot be mixed with links and docker-compose gives the following error message:

lb_1   | [ALERT] 208/120346 (9) : parsing [/haproxy.cfg:41] : 'server tsi-api_plumber_1' : could not resolve address 'tsi-api_plumber_1'.

I tried different things but it didn't work. Can someone help with adapting the docker-compose.yml to tell the load balance which service to balance?

Matthias Munz
  • 3,583
  • 4
  • 30
  • 47
  • `links:` are obsolete and you shouldn't need them at all; `network_mode: host` generally disables Docker networking and means you won't be able to talk to the other container. Do you need to add MongoDB to this container stack, or to use one of the techniques in [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) to connect to a process on the host system? – David Maze Jul 28 '21 at 12:58
  • The MongoDB is running on another server to which I have set up a tunnel with `autossh -M 20000 -L 9999:localhost:27017 @ -vN -p22`. I.e. I want to access localhost:9999 from within my plumber Docker container. – Matthias Munz Jul 28 '21 at 13:10
  • The local system, the remote server, and the application container each think they're `localhost`. You might need to set the `ssh -L` tunnel listener to listen somewhere other than the physical host's localhost interface, because of details of the Docker networking layer. – David Maze Jul 28 '21 at 13:12
  • Regarding `links`, I haven't found another solution so far to tell dockercloud/haproxy which service to balance. – Matthias Munz Jul 28 '21 at 13:17
  • Thanks David. I could make it work by adding the docker host IP address on the docker0 network interface `172.17.0.1` to the tunnel to `ssh -L 172.17.0.1:9999:localhost:27017 @ -vN -p22` and also in my R script mongo(...url = "mongodb://172.17.0.1:9999"...). No `network_mode: host` is needed. Still, I couldn't find any alternative for `links` yet that works with dockercloud/haproxy. – Matthias Munz Jul 28 '21 at 13:57

1 Answers1

0

I basically followed the instructions by David (see comments above) and at From inside of a Docker container, how do I connect to the localhost of the machine?

  1. Get Docker host IP address on docker0 network interface (in this example 172.17.0.1) with ip addr show docker0
  2. Establish SSH tunnel with ssh -L 172.17.0.1:9999:localhost:27017 <user>@<mongodb-server> -vN -p22
  3. In my plumber R script I changed the Mongo URL to mongo(...url = "mongodb://172.17.0.1:9999"...)

No network_mode: host is needed.

Matthias Munz
  • 3,583
  • 4
  • 30
  • 47