postgresql - 连接到 docker 中托管的 postgresql 时出现问题
问题描述
我在连接到 docker 中托管的 postgresql 数据库时出现以下错误。
myapp_Container | WARNING - ConnectionBus: Database connections: 0 active, 0 idle.
myapp_Container | ERROR - ConnectionBus: Opening JDBC connection to Some(db:54325) failed with SQLState: 08001 Error code: 0 Message: The connection attempt failed., giving up...(4/4)
myapp_Container | WARNING - ConnectionBus: Cannot create database connection.
我有两个 docker-compose 文件如下
APP Docker-撰写文件
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
network_mode: postgres_net
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd@db:54325/mydb
external_links:
- db:localpostgres_1
DB Docker-撰写文件
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
我通过创建 postgres_netdocker network create -d bridge postgres_net
我为什么要这样做?我想创建具有相同 docker-compose 结构的应用程序,而不为每个应用程序创建一个 postgresql 数据库,如果我像下面这样将它们放在一起,它将起作用。
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd@db/mydb
links:
- db
db:
image: postgres
container_name: postgres_x
ports:
- "54320:5432"
environment:
- TZ=Europe/Amsterdam
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
restart: always
我可以通过 pgadmin 连接到 Db,但不明白为什么 myapp 无法连接,请帮忙
pg_hba.conf
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all all md5
解决方案
您尚未在 First compose 文件(App compose)中正确定义网络。您已指定postgres_net
哪个network_mode: postgres_net
无效。您需要像在第二个撰写文件中一样正确指定它。
更正的撰写文件将是:
APP Docker-Compose
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd@db:54325/mydb
networks: # declare network here
- postgres_net
networks: #define network
postgres_net:
external: true
DB Docker-Compose
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
推荐阅读
- ffmpeg - FFMPEG - filter_complex lut 文件路径在 Windows 上出现问题
- html - HTML5 图像不会显示
- azure - Windows 7 设备的混合 Azure AD 加入问题
- java - Android Widget - 单击按钮时更新
- python - 如何在张量流概率中运行线性回归,其中参数遵循不同的分布
- c# - 如何在要从 Conf 文件中读取的 Google Map URL API 密钥字符串中添加变量
- android - ConstraintLayout - wrap_content + toRightOf 布局问题
- javascript - 具有覆盖的 Div 在 Chrome 中不滚动
- sql - 有没有办法(例如窗口函数)选择具有不同数量的行并按列分区?
- azure-devops - Azure DevOps Pipeline 没有复制或部署我的所有项目