【发布时间】:2018-06-29 19:24:25
【问题描述】:
我正在尝试构建一个由 Spring Boot 应用程序和 PostgreSQL 数据库组成的“服务”。当 Spring Boot 应用程序在我的本地计算机上运行时,我已经能够从 Spring Boot 应用程序访问数据库(在容器中运行)。现在,当我尝试将 Spring Boot 应用程序移动到容器时,我收到以下错误:
inventory_1 | 2018-01-20 18:43:06.108 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
inventory_1 |
inventory_1 | java.net.ConnectException: Connection refused (Connection refused)
但是,我可以从本地计算机连接到数据库:
psql -h localhost -p 5000 -U kelly_psql -d leisurely_diversion
我的 application.properties 文件:
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://localhost:5432/leisurely_diversion
spring.datasource.username=kelly_psql
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver
我的 docker-compose 文件:
# Use postgres/example user/password credentials
version: '3.2'
services:
db:
image: postgres
ports:
- 5000:5432
environment:
POSTGRES_PASSWORD: example
volumes:
- type: volume
source: psql_data
target: /var/lib/postgresql/data
networks:
- app
restart: always
inventory:
image: kellymarchewa/inventory_api
depends_on:
- db
ports:
- 8080:8080
networks:
- app
restart: always
volumes:
psql_data:
networks:
app:
我的 Dockerfile(来自Spring website)
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
我怀疑问题在于(就我而言)对 Docker 或容器的误解,但我不确定。任何建议将不胜感激。
【问题讨论】:
标签: spring postgresql docker spring-boot docker-compose