【问题标题】:Spring Boot Docker Container can't connect to postgresql hosted on Docker hostSpring Boot Docker Container 无法连接到托管在 Docker 主机上的 postgresql
【发布时间】:2018-12-19 14:01:53
【问题描述】:

当我使用 maven 构建我的 Spring Boot 应用程序时,应用程序需要在 application.properties 中连接到我的 postgresql 数据库。只有在我使用时才能连接

spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb

但是当我将构建的 jar 文件容器化时,我的应用程序无法连接到容器外部托管的 postgres 数据库。

这个问题的解决方法是什么?

我还是个初学者

【问题讨论】:

  • 您使用的是 Windows 吗?
  • 是的,我使用的是 Windows 10

标签: postgresql maven docker spring-boot


【解决方案1】:

来自 18.03 文档:

我想从容器连接到主机上的服务

主机有一个不断变化的 IP 地址(如果您没有网络,则没有 使用权)。从 18.03 起,我们的建议是连接到 特殊 DNS 名称 host.docker.internal,解析为内部 主机使用的 IP 地址。

网关也可以通过 gateway.docker.internal 访问。

示例:

spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/springbootdb

【讨论】:

【解决方案2】:

您在哪里托管 PostgreSQL 数据库?

将localhost替换为实际机器IP地址(PostgreSQL数据库机器地址)

spring.datasource.url=jdbc:postgresql:<ip_address>:5432/springbootdb

【讨论】:

  • 对我不起作用:java.net.UnknownHostException: postgresql 当我想用 maven 构建时
  • 您的 PostgreSQL 数据库在哪里?在安装 docker 的同一台机器上?还是外面? (windows?) 以及 docker 应用程序在哪里 (windows/ubuntu?)
  • 它在我的 Windows 本地主机上,但我认为容器有另一个环境。与 docker right 相同的机器
  • 您的防火墙是否阻止传入/传出端口 5432?您能否在您的 Windows 防火墙上添加/允许进出 5432?
  • 不幸的是它对我不起作用,谢谢您的建议
猜你喜欢
  • 2019-01-29
  • 1970-01-01
  • 2020-07-31
  • 2018-09-16
  • 1970-01-01
  • 2020-10-20
  • 2020-07-12
  • 2020-07-24
  • 1970-01-01
相关资源
最近更新 更多