【发布时间】:2021-04-12 18:50:37
【问题描述】:
Docker-compose 有 2 个容器。第一个是 Postgres 数据库,第二个是 Java Spring Boot 应用程序。为了运行,我使用了更多的 docker-compose 配置文件:
docker-compose.yml
version: "3.7"
services:
db-service:
image: postgres
restart: always
volumes:
- /home/ec2-user/dbdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1
POSTGRES_DB: auth
ports:
- 5432:5432
auth-service:
build: .
restart: always
depends_on:
- db-service
links:
- db-service
ports:
- 80:80
我想使用/home/ec2-user/dbdata 来包含数据库数据,毕竟数据是创建的。成功地。 postgres 容器的日志是:
PostgreSQL 初始化进程完成;准备开始 2021-01-07 01:36:16.786 UTC [1] 日志:启动 PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) x86_64-pc-linux-gnu,由 gcc (Debian 8.3.0-6) 8.3.0 编译,64 位 2021-01-07 01:36:16.786 UTC [1] 日志:监听 IPv4 地址“0.0.0.0”,端口 5432 2021-01-07 01:36:16.786 UTC [1] 日志:侦听 IPv6 地址“::”,端口 5432 2021-01-07 01:36:16.790 UTC [1] 日志:监听 Unix 套接字“/var/run/postgresql/.s.PGSQL.5432” 2021-01-07 01:36:16.795 UTC [76] 日志:数据库系统已关闭 在 2021-01-07 01:36:16 UTC 2021-01-07 01:36:16.800 UTC [1] LOG:数据库系统已准备好接受连接
但是 Java 应用抛出错误:
org.postgresql.util.PSQLException:连接到 127.0.0.1:5432 拒绝了。检查主机名和端口是否正确,以及 postmaster 正在接受 TCP/IP 连接。
但在端口映射中是5432:5432。
而Java app中的数据源属性是:
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/auth
spring.datasource.username=postgres
spring.datasource.password=1
这个错误的原因是什么?
【问题讨论】:
标签: java postgresql spring-boot docker