简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM