Aditya Acharya
Aditya Acharya

Reputation: 1

How to access non containerised DB on remote IP from a docker container?

I am trying to package Java web services in Docker. I have a postgres DB hosted on a VM (non-containerised) and the code in the docker container is unable to connect to the database. How to do that?

Upvotes: 0

Views: 95

Answers (1)

asgarov1
asgarov1

Reputation: 4098

Theoretically spring.datasource.url= jdbc:postgresql://yourIpAddress/nameOfDB should work.

But database don't run on 8080 so you need to bind the port of database (for postgres 5432, for mysql 3306) and I wouldnt bind it to 80, where everything is listening.

If you are running postgres it will be listening on 5432 so you can do docker run -p 8082:5432 postgres

Then you should be able to connect to your host computer via jdbc:postgresql://yourIpAddress:8082/nameOfDB

This all assumes nothing else jumps in the way, like firewalls or whatnot. I also don't know how you configured your virtual machine. In general you should practice connecting them on the same machine to get the idea first.

Upvotes: 1

Related Questions