Spring Boot,PostgreSQL和Docker-在容器中运行时拒绝连接 [英] Spring Boot, PostgreSQL, and Docker - Connection Refused whil Running in Container

查看:158
本文介绍了Spring Boot,PostgreSQL和Docker-在容器中运行时拒绝连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个由Spring Boot应用程序和PostgreSQL数据库组成的服务。当Spring Boot应用程序在本地计算机上运行时,我已经能够从Spring Boot应用程序访问数据库(在容器中运行)。现在,当我尝试将Spring Boot应用程序移动到容器时,收到以下错误:

I am attempting to build a "service" consisting of a Spring Boot application and PostgreSQL database. I have been able to access the database (running in a container) from the Spring Boot app while the Spring Boot application was running on my local machine. Now, when I attempt to move the Spring Boot application to a container, I am received the following error:

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

However, I am able to connect to DB from my local machine: psql -h localhost -p 5000 -U kelly_psql -d leisurely_diversion

我的application.properties文件:

My application.properties file:

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文件:

My docker-compose file:

    # 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网站

My Dockerfile (from the 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或容器的误解(对我而言),但我不确定。任何建议将不胜感激。

I suspect the issue lies in a misunderstanding (on my part) of Docker or containers, but I am not sure. Any advice would be appreciated.

推荐答案

您正在将应用程序指向 localhost ,但不会在容器之间共享。

You are pointing your application towards localhost, but this is not shared between containers.

要访问另一个容器,您必须引用其主机名

To access another container you have to refer to its hostname.

对于您而言,我了解到您希望库存服务访问 db 服务。因此,您应该使用以下 datasource 网址:

In your case, I understand that you want the inventory service to access the db service. So you should use the following datasource url:

spring.datasource.url=jdbc:postgresql://db:5432/leisurely_diversion

请参阅以下有关连接到来自具有docker的另一个容器的容器: https://docs.docker.com/compose/gettingstarted/

See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/

这篇关于Spring Boot,PostgreSQL和Docker-在容器中运行时拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆