2

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:[email protected]: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

2 Answers 2

1

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:

  1. Find osx Ip local address in Preferences > Network Settings (my was 192.168.0.102)
  2. Add this Ip to line listener_address: '192.168.0.102, localhost' of postgresql config file.
  3. Add extra line with Ip 192.168.0.102, host and md5 to pg_hba.conf file
  4. Restart Postgresql

Set connection settings in app via ENV like: postgres://postgres:[email protected]:5432/movies_development

Sign up to request clarification or add additional context in comments.

1 Comment

0

There may be two reasons as per your question :

  • Have you exposed 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.
  • If your are going inside your docker container with docker exec -it $container_name bash and executing command this psql "postgres://postgres:[email protected]: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.
  • Note: if your application is using some other way to communicate with database than your application will work without psql client program.

Hope this help.Thank you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.