postgresql - Connect containerized .NET Core MVC/WebAPI app to LOCALLY installed Postgres -
i've been searching couple of days , can't solution.
i'm using .net core 1.1 , vs2017 docker tools. when run app self-hosted option or iis ok. i'm using ngpsql data provider postgresql , thing works great. problem appears when run app container docker (the image build nice, container runs in windows , linux) because postgres host localhost , core app can't see local postgresql instance (because container localhost container , not host machine). know there lots of tutorials , walkthroughs .net core , psql through docker compose (a container each one), know that. assume work great requirements job de containerized core app read/writes data locally installed postgresql server.
in production, core container , locally installed postgresql in same machine same address. know must simple parameter thing or that, don't want lose portability of docker container using in connection string (development) machine ip because deploy/run scenario in production server automatized via docker cloud , stuff.
maybe passing parameter? exposing docker container port? what? whaaat?!
you can add code entrypoint
:
echo $(netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}') dockerhost >> /etc/hosts
the script gets gateway ip address inside container. because container (probably) accesses internet through host machine, gateway ip host machine's ip on whatever network host , container share. appends entry in containers hosts file scripts can refer host ip name (arbitrarily 'dockerhost' in case).
running script in entry point let's right ip address if deploy image on docker host different network configuration because hosts entry set when container runs, not when image built
Comments
Post a Comment