当我使用 maven 构建我的 spring boot 应用程序时,该应用程序需要在 application.properties 中连接到我的 postgresql 数据库。它只有在我使用时才能连接
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb
但是当我将构建的 jar 文件容器化时,我的应用程序无法连接到容器外托管的 postgres 数据库。
这个问题的解决方案是什么?
我还是个初学者
最佳答案
来自 18.03 文档:
I want to connect from a container to a service on the host
The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.
The gateway is also reachable as gateway.docker.internal.
例子:
spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/springbootdb
关于postgresql - Spring Boot Docker 容器无法连接到 Docker 主机上托管的 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51287001/