I have a working setup of Spring boot application connecting to Postgres DB in docker. The same Spring boot application throws an exception when I move it to another docker container. The Postgres docker was unchanged. What might be wrong ? Why is the same application working outside docker and not connecting to Postgres when put inside docker.
org.postgresql.util.PSQLException: Connection to MYDOMAIN:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
application.properties
spring.datasource.url=jdbc:postgresql://MYDOMAIN:5432/
UPDATE
When I changed MYDOMAIN to the public IP address of the machine hosting Postgres docker, it worked fine. But why is the domain name not getting resolved ?
2
Answers
I could find the root cause finally. From /etc/hosts file, when I removed MYDOMAIN against 127.0.0.1, the spring boot application was able to resolve MYDOMAIN via internet and access Postgres.
Because a Docker Container is an isolated environment which you only have your spring boot application in it. so inside that container, there are no Postgres running on port 5432.
you can follow the instruction in this link to create a docker-compose file in which you can address the PostgreSQL Docker Container in that file to your dockerized spring boot application.