1

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

Hard Worker
  • 995
  • 11
  • 33

2 Answers2

1

Changes the url from localhost to

host.docker.internal

Meaning:

  dw.url=jdbc:postgresql://host.docker.internal:5432/DW
Hard Worker
  • 995
  • 11
  • 33
0

EXPOSE acts as documentation, just like comments in the Dockerfile. You need to use -p flag in the docker run command. See the docs for more details: https://docs.docker.com/engine/reference/builder/#expose

oartart
  • 116
  • 1
  • 2
  • Some tools hook into EXPOSE, though. So it's a bit more than documentation. It's more like a label. – The Fool Apr 27 '22 at 08:18
  • @oartart didnt solve the issue, still failing with the same error docker run -p 5432:5432/tcp -p 5432:5432/udp ec0bdea074a6 – Hard Worker Apr 27 '22 at 08:25