Background
I‘ve started a hobby project to work with Springboot and Postgres, where I created a service which connects to a PostgresDB on Render.com. Both my app/service and the DB are running on it.
I‘ve tried to connect them both, adding a url in my application.yml
where I’ve used the URL generated by render (something like postgressomethingrandomstring.com) and when starting it over the IDE everything works fine, however when starting inside a docker container I get the error The connection attempt failed
.
The solution
Docker containers communicate with each other and with external hosts through virtual networks that have their own IP addresses. These IP addresses are not visible or accessible from outside the Docker network, unless you map a port from your container to your host machine.
If you want to access an external database from a Docker container, you need to use the IP address. So I’ve just used ping postgressomethingrandomstring.com
where I saw the IP address of my postgresDB on render and set that in my application.yml
as url. (example: url: "jdbc:postgresql://18.193.138.22:5432/dbname"
)
Using IP address instead of hostname might be your solution to the problem when the DB is not in the same network as your Docker app. Hope that helps someobody out too.. and saves them 2 HOURS!!! of debugging this issue..
Cheers!