skip to Main Content

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


  1. Chosen as BEST ANSWER

    Changes the url from localhost to

    host.docker.internal
    

    Meaning:

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

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search