postgresql - Spring Boot Docker 容器无法连接到 Docker 主机上托管的 postgresql

标签 postgresql maven docker spring-boot

当我使用 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/

相关文章:

ruby-on-rails - 如何从多选中存储数组

postgresql - PREPARE TRANSACTION 释放锁?

java - 如何设置从 Red Hat BPM Suite 6.4 导入的 JBoss Dev Studio 项目来构建 Java

linux - 如何存储已经通过管道的命令的输出

docker - 构建器的 Gitlab CI 拉取访问被拒绝,存储库不存在或可能需要 'docker login'

sql - 在 Order by 子句中使用 date_part 函数总是返回列必须出现在 Group by 子句中

Django + 大型数据库 : how to deal with 500m rows?

Ubuntu 14.04 Jamod 串行上的 java.lang.UnsatisfiedLinkError : no rxtxSerial in java. library.path

java - 在 Netbeans 中找不到 Maven

laravel - 如何为我的数据库用户设置一次密码?