Docker container image cant access postgresql DB Connection to localhost:5432 refused

Viewed 353

I'm running a PostgreSQL as a native app(NOT CONTAINER) on my localhost I'm able to connect to it with an SQL client.

While I'm running a spring application - in a container the app starts and fails while trying to connect the database

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. 
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

in my docker image, I have used expose to open the port, but it still won't work

EXPOSE 8080 8443 4000 5432

What can i do to fix it

I run my image with

docker run ec0bdea074a6

Tried also

docker run -p 5432:5432/tcp -p 5432:5432/udp ec0bdea074a6

And it didnt work as well

2 Answers

Changes the url from localhost to

host.docker.internal

Meaning:

  dw.url=jdbc:postgresql://host.docker.internal:5432/DW
Related