0

I've been searching this a couple of days and I can't get a solution.

I'm using .NET Core 1.1 and VS2017 with Docker Tools. When I run the app with self-hosted option or IIS everything is OK. I'm using Ngpsql as data provider for PostgreSQL and the thing works great. The problem appears when i run the app as a container with Docker (the image build nice, the container runs in Windows and Linux) because my Postgres Host is localhost and the Core app can't see my local PostgreSQL instance (because for the container localhost is the container itself and not my host machine). I know there are LOTS of tutorials and walkthroughs to do this with .NET Core and PSQL through Docker Compose (a container for each one), I know that. I assume they work great BUT the requirements for this job is that de containerized Core app read/writes data from a locally installed PostgreSQL server.

In production, the Core Container and the LOCALLY installed PostgreSQL will be in the same machine with the same address. I know this must be a simple parameter thing or something like that, but I don't want to lose portability of the Docker Container by using in the connection string my (development) machine ip because the deploy/run scenario in the production server will be automatized via Docker Cloud and stuff.

Maybe passing a parameter? Properly exposing docker container port? What? WHAAAT?!

Daniel Díaz Astudillo
  • 1,622
  • 2
  • 14
  • 14
  • 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) – Ayushya Jul 26 '17 at 03:02
  • Thanks @Ayushya, going to test that answer. – Daniel Díaz Astudillo Jul 26 '17 at 16:07
  • In fact what does the job is this answer https://stackoverflow.com/questions/31249112/allow-docker-container-to-connect-to-a-local-host-postgres-database. Please mark as duplicate or something like that (i don't know the exact term). I've followed those steps and works. – Daniel Díaz Astudillo Aug 03 '17 at 21:41
  • Possible duplicate of [Allow docker container to connect to a local/host postgres database](https://stackoverflow.com/questions/31249112/allow-docker-container-to-connect-to-a-local-host-postgres-database) – Ayushya Aug 04 '17 at 02:43

1 Answers1

0

You can add this code to your entrypoint:

echo $(netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}') dockerhost >> /etc/hosts

The script gets the gateway IP address from inside the container. Because your container (probably) accesses the internet through the host machine, the gateway IP will be the host machine's IP on whatever network the host and the container share. It then appends an entry in the containers hosts file so your scripts can refer to the host IP by name (arbitrarily 'dockerhost' in this case).

Running the script in the entry point let's you get the right IP address even if you deploy an image on a docker host with a slightly different network configuration because the hosts entry is set up when the container runs, not when the image is built

Source: Reddit. For more info see here

Ayushya
  • 9,599
  • 6
  • 41
  • 57