我正在尝试将打包在 .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.
Docker-compose 文件:
---
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