1

I have simple docker-compose aka:

version: '3'
services:
  app:
    container_name: app
    ports:
      - 8081:8081
  db:
    container_name: db
    ports:
      - 5432:5432

And by default this containers are created in default(brige) network. The app has db connection property: jdbc:postgresql://db/some_db, and everythig works perfectly. But from time to time I want the app to connecto to other db, that is running on my host machine, not in docker container.

The main problem is that I can not change my connection properties. And, ideally, I do not want to run new container with some additional options every time I want to switch the db host (but restart is ok)

Hence my question: what is the best way to achive this? Is it possible to set up additional route for containers host resolving? For exapmle, if db container is unreachable, then route to host.

Green A
  • 11
  • 4
  • Possible duplicate of [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) – lifeisfoo Oct 12 '17 at 20:37

1 Answers1

0

You can access host services from your host.
See "How to access host port from docker container":

ip addr show docker0 
docker.for.mac.localhost # docker 17.06+ June 2017

if db container is unreachable, then route to host.

That is a job for an orchestrator.
For instance, with kubernetes, you can associate an external load balencer, which could be tuned to redirect traffic to your pod, unless it is not accessible.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250