简体   繁体   English

如何从 docker 容器访问远程 IP 上的非容器化数据库?

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

I am trying to package Java web services in Docker.我正在尝试 package Java web 服务在 ZC5FD214CDD0D2B3B54272E73B02. 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.我有一个托管在 VM(非容器化)上的 postgres 数据库,并且 docker 容器中的代码无法连接到数据库。 How to do that?怎么做?

Theoretically spring.datasource.url= jdbc:postgresql://yourIpAddress/nameOfDB should work.理论上spring.datasource.url= jdbc:postgresql://yourIpAddress/nameOfDB应该可以工作。

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.但是数据库不在8080上运行,所以你需要绑定数据库的端口(对于postgres 5432,对于mysql 3306),我不会将它绑定到80,一切都在监听。

If you are running postgres it will be listening on 5432 so you can do docker run -p 8082:5432 postgres如果您正在运行 postgres,它将在 5432 上侦听,因此您可以执行docker run -p 8082:5432 postgres

Then you should be able to connect to your host computer via jdbc:postgresql://yourIpAddress:8082/nameOfDB然后您应该能够通过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.一般来说,您应该练习在同一台机器上连接它们以首先获得想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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