我有一个安装了PostgreSQL的服务器。所有的服务都在容器中运行(使用docker-compose)。我想从容器中使用我的主机上的PostgreSQL,但是出现了错误:
Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/shop-bd) for user 'shop-bd-user': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State : 08001
Error Code : 0
Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused (Connection refused)
我的docker-compose正在使用主机网络模式,就像这样:
version: '3'
services:
shop:
container_name: shop
build: ./shop
hostname: shop
restart: always
ports:
- 8084:8084
network_mode: "host"
我的数据库连接URL是:jdbc:postgresql://localhost:5432/shop-bd
8084:8084
映射,但你没有提及端口5432
。 - jimmu8084
是该服务的端口。其他服务可以通过http://localhost:8084
与此服务通信。但对于主机DB不起作用。 - Maxdb
服务的配置是什么样子的。它应该在端口下面有5432:5432
。 - jimmu