Java Docker compose up springboot+;postgres连接被拒绝
我使用springboot数据和postgres创建了一个示例java项目。我开始让我的docker编写,但我无法将我的数据库链接到我的应用程序 我有这个错误Java Docker compose up springboot+;postgres连接被拒绝,java,postgresql,docker,Java,Postgresql,Docker,我使用springboot数据和postgres创建了一个示例java项目。我开始让我的docker编写,但我无法将我的数据库链接到我的应用程序 我有这个错误 web_1 | org.postgresql.util.PSQLException: Connection to localhost:5438 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP
web_1 | org.postgresql.util.PSQLException: Connection to localhost:5438 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
关于我的申请
server:
port: 4000
spring:
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5438/acronym
username: acronym
password: acronym1234
driver-class-name: org.postgresql.Driver
jpa:
database: POSTGRESQL
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: create
和我的docker一起写文件
version: '3.3'
services:
web:
image: acronym:latest
ports:
- 4000:4000
networks:
- webnet
depends_on:
- db
db:
image: postgres:9.6
restart: always
environment:
- POSTGRES_USER=acronym
- POSTGRES_PASSWORD=acronym1234
- POSTGRES_DB=acronym
-PGDATA=/var/lib/postgresql/data/pgdata
volumes:
- /var/lib/postgresql/data
ports:
- '5438:5432'
volumes:
db:
networks:
webnet:
我看到了很多主题,解决方案是将localhost替换为db,但这不起作用
如何使用docker compose将我的数据库链接到我的应用程序 使用
jdbc:postgresql://db:5438/acronym
作为连接url
默认情况下,该服务以其名称公开(在这种情况下,
db
)。谢谢您的回复!是的,我试图在我的applocation资源中将localhost替换为db,但我遇到了相同的问题:(好的,我用db和端口5432再试一次……结果成功了!!THX YOUYou映射了docker网络(webnet)内的端口它在端口5438下和5432上的本地主机上可用。端口:-“5438:5432”
您可以使用相同的端口以避免混淆=>端口:-“5438:5438”