java - 无法从另一个容器连接到 docker 容器。(org.postgresql.util.PSQLException:连接到 localhost:5432 被拒绝)
问题描述
我正在尝试将打包在 .jar 中的 springboot 应用程序放入 docker 容器中,并使用与另一个 docker 容器内的数据库的连接。从 Idea 启动应用程序时(当 db 已经在 docker 容器上运行时)一切正常。但是,当 .jar 在容器内时,它无法按预期工作,并返回下一个错误:
application | 2021-04-14 09:19:19.628 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : Rehabilitation - Starting...
application | 2021-04-14 09:19:20.668 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : Rehabilitation - Exception during pool initialization.
application |
application | 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.
码头工人撰写文件:
---
version: "3.8"
volumes:
database_volume:
services:
database:
image: postgres:13
container_name: database_Rh
tty: true
environment:
POSTGRES_PASSWORD: "1243"
POSTGRES_DB: "Rehabilitation"
POSTGRES_USER: "postgres"
volumes:
- type: volume
source: database_volume
target: /var/lib/postgresql/data
expose:
- 5432
ports:
- "5432:5432"
application:
image: newer_clinic
container_name: application
tty: true
expose:
- 8080
ports:
- "8080:8080"
spring application.properties 文件如下所示:
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/
spring.datasource.name=Rehabilitation
spring.datasource.username=postgres
spring.datasource.password=1243
docker ps 命令返回下一个(由于无法连接到数据库,带有应用程序的容器在启动几秒钟后关闭)
PS C:\Users\Антон> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1eb7ca964e3f newer_clinic "java -jar ./clinic_…" 16 minutes ago Up 1 second 0.0.0.0:8080->8080/tcp application
7ede33a7d296 postgres:13 "docker-entrypoint.s…" 16 minutes ago Up 16 minutes 0.0.0.0:5432->5432/tcp database_Rh
解决方案
推荐阅读
- django - 用户登录后如何在导航栏中的登录链接上显示用户名
- laravel - 我正在尝试在 Laravel 中添加关系。它自己加入一对多关系
- android - Android ProGuard androidx 保持受保护的方法,但发布是公开的
- php - 为什么 ImageColorSet() 在 PHP 中不起作用?
- php - 如何在php中更改json数组格式?
- apache-kafka-streams - Kafka Streams 应用程序无法以 Cloud Foundry 中的“what():资源暂时不可用”开始
- tensorflow - 如何固定编码器/解码器的神经元数量?
- python - 我如何从