I have a docker container with web rails app. Also I have external database on my osx machine. If I exec on osx:
psql "postgres://postgres:testing@0.0.0.0:5432/movies_development"
I run okay
If I run it in container I'll receive
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Have any ideas how to connect to external database from container? thanks
I found a way for connect app in docker container to local database which located on my osx.
0.0.0.0:5432 is an localhost address of container (not a localhost of osx) and we must set new one:
listener_address: '192.168.0.102, localhost' of postgresql config file.Set connection settings in app via ENV like: postgres://postgres:testing@192.168.0.102:5432/movies_development
There may be two reasons as per your question :
EXPOSE 5432 during your docker image build ? or before starting container with docker run -it -d --name $container_name -p 5432:5432 $image_name ? If NO than you must have to do any of above changes than and than it can connect to externally hosted database. docker exec -it $container_name bash and executing command this psql "postgres://postgres:testing@0.0.0.0:5432/movies_development" then there must be psql client installed in your docker container with proper $PATH variable otherwise it will throw error like command not found/no such file or directory.Hope this help.Thank you!