0

I struggle to achieve the following:

I want two have two docker compose services (let's call them service_A and service_B), where

  • service_A needs to be in the host's network (i.e. runs with network_mode=host) and
  • service_B can connect to service_A via its hostname, but it's also in the hosts network.

Here I made an example, which demonstrates what I want to achieve with postgres services as examples:

version: '3'

services:
  service_A:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=password
    network_mode: host  

  service_B:
    image: postgres
    depends_on:
      - service_A
    network_mode: host 
    command: psql postgresql://postgres:password@service_A:5432 --command "select * from pg_tables limit 1;"

But, when setting network_mode=host on service_A and service_B, service_B cannot reach service_A via its hostname. Is there an easy way to achieve that?

For those, who are interested why I need such a setup:

  • service_A functions as a proxy and logs into a VPN (via the docker host's connection)
  • and service_B shouldn't see the whole docker host, but needs to read from service_A, but be also responsive on the docker host.
physicus
  • 361
  • 4
  • 12
  • If you bash into service_b can you ping service_a? – nateleavitt May 27 '21 at 19:28
  • `network_mode: host` generally disables Docker's networking layer; you can't combine it with other network options. If `service_B` ran on a normal Docker bridge network, it could connect to `service_A` the same as any other host-based process. – David Maze May 27 '21 at 21:25

0 Answers0