0

I use Ubuntu 18, have an non-container Postgres database, installed on my machine, that I would like to connect from Docker container.

My current docker-compose.yaml:

version: "3"

services:
    ws-server:
        image: ws-server
        networks:
            - net-ws
        ports:
            - 2020:2020
    shortest-path:
        image: ws-client
        networks:
            - net-ws
        expose:
            - 5000
networks:
    net-ws:

Here I would like to add a configuration to be able to reach host database.

When I run my application with:

docker run --rm -it --network=host ws-server

then all works properly.

So how to configure docker-compose.yaml to access host Postgres in Ubuntu 18?

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
  • use host ip from within the container, or setup a bridge and use that as the network. [access host service from docker](https://www.google.com/search?q=access+host+service+from+docker+site:stackoverflow.com) ... [etc](https://nickjanetakis.com/blog/docker-tip-35-connect-to-a-database-running-on-your-docker-host) – Lawrence Cherone Aug 07 '19 at 15:20
  • @LawrenceCherone I would think that should be much easier solution than these manipulations with docker0 interface – Kirill Ch Aug 07 '19 at 15:24
  • thats just one way, did you see https://nickjanetakis.com/blog/docker-tip-35-connect-to-a-database-running-on-your-docker-host `--net` can be defined in your compose file, your just not defining it – Lawrence Cherone Aug 07 '19 at 15:25
  • @LawrenceCherone Yes, this is a useful material. Thank you. If you give an exact answer it will be great (even if it is not about yaml file). – Kirill Ch Aug 07 '19 at 15:28
  • 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) – David Maze Aug 07 '19 at 15:56

1 Answers1

0

As I see when use host mode there is no needs for other networks as all service ports are published. Also networks and network_mode can not be used together.

So the docker-compose.yaml that works:

version: "3"

services:
    ws-server:
        image: ws-server
        network_mode: host
        ports:
            - 2020:2020
    shortest-path:
        image: ws-client
        network_mode: host
        expose:
            - 5000

Now I can successfully access host Postgres database. However I would prefer access host database using also private (not host) network so that not publish unnecessary ports.

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65