无法从 wsl 2 上运行的服务之一连接到 wsl 2 上的容器中运行的 postgres 数据库
我无法从 wsl 2 上运行的一项服务连接到 wsl 2 上的容器中运行的 postgres 数据库。
我可以从 wsl 2 上运行的另一个服务连接到该数据库,也可以从运行的 pgAdmin 连接到该数据库在 Windows 上以及在 Windows 上运行的另一个服务。
2022-02-25 20:25:52.975 INFO 2829 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-25 20:25:53.207 INFO 2829 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
但是,当我尝试从 wsl 2 上运行的另一个服务连接到此数据库时,
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
我尝试使用系统的 WSL IP、ipv4 代替 localhost 没有任何帮助。两个服务之间的区别首先是 uber jar,其次我尝试使用 docker-compose 运行。
I am unable to connect to postgres DB running in container on wsl 2 from one of the service running on wsl 2.
I can connect to this DB from one another service running on wsl 2 as well as I can connect to this DB from pgAdmin running on windows and another service running on windows.
2022-02-25 20:25:52.975 INFO 2829 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-25 20:25:53.207 INFO 2829 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
But when I try to connect to this DB from another service running on wsl 2 I get
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
I tried using WSL IP, ipv4 of system in place of localhost nothing helps. The difference between 2 services is first is uber jar and second I am trying to run using docker-compose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试从本地主机访问数据库,则需要发布数据库的端口。
如果您尝试从另一个容器进行连接,则需要将两个容器加入同一 docker 网络上,并使用数据库容器的名称作为主机名而不是 localhost。或其别名之一。在组合的情况下,容器通常已经位于同一网络上,并且您可以使用组合服务名称作为主机名,因为该服务的所有副本都以服务名称的别名添加到网络中。
In case you are trying to access the DB from localhost, you need to publish the ports of the DB.
In case you are trying t connect from another container, you need to join both container on the same docker network and use the DB container's name as hostname instead of localhost. Or one of its aliases. In case of compose, the container are usually already on the same network, and you can use the compose service name as hostname since all replicas of that service are added to the network(s) under an alias that is the name of the service.