无法将 dockerized spring boot 应用程序连接到 dockerized postgresql

Posted

技术标签:

【中文标题】无法将 dockerized spring boot 应用程序连接到 dockerized postgresql【英文标题】:Cannot connect dockerized spring boot app to dockerized postgre sql 【发布时间】:2017-12-29 02:01:39 【问题描述】:

我有一个包含两个服务的 docker-compose.yml:

version: '2'
services:

  stuffer:
    container_name: stuffer_container
    build: .
    ports:
     - "8080:8080"
    environment:
     - spring.profiles.active=dev
    depends_on:
     - postgreDB

  postgreDB:
    container_name: postgreDB_container
    image: "postgres:9.6.2"
    ports:
     - "5432:5432"
    expose:
     - "5432"
    environment:
     - POSTGRES_DB=stuffer
     - POSTGRES_USER=postgres
     - POSTGRES_PASSWORD=root123
    volumes:
     - /home/iwaneez/postgreData:/var/lib/postgresql/data

我的应用有 application-dev.properties:

## Datasource config
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://postgreDB:5432/stuffer
spring.datasource.username=postgres
spring.datasource.password=root123

当我使用

运行它们时

码头工人组成

我收到连接被拒绝异常:

stuffer_container | Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container |     at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:60) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:192) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container |     at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:92) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container |     at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:83) ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
stuffer_container |     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
stuffer_container |     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
stuffer_container |     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
stuffer_container |     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
stuffer_container |     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     ... 61 common frames omitted
stuffer_container | Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container |     at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:46) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     ... 71 common frames omitted
stuffer_container | Caused by: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
stuffer_container |     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:138) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:32) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.Driver.makeConnection(Driver.java:393) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.Driver.connect(Driver.java:267) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.11.jar!/:na]
stuffer_container |     at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
stuffer_container |     ... 72 common frames omitted
stuffer_container | Caused by: java.net.ConnectException: Connection refused (Connection refused)
stuffer_container |     at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_131]
stuffer_container |     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_131]
stuffer_container |     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_131]
stuffer_container |     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_131]
stuffer_container |     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.<init>(Socket.java:434) ~[na:1.8.0_131]
stuffer_container |     at java.net.Socket.<init>(Socket.java:211) ~[na:1.8.0_131]
stuffer_container |     at org.postgresql.core.PGStream.<init>(PGStream.java:62) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:76) ~[postgresql-9.1-901-1.jdbc4.jar!/:na]
stuffer_container |     ... 91 common frames omitted

如何将应用程序连接到在 docker 中运行的 postgre?

我还尝试将 ubuntu 映像作为第三个服务运行,并且可以使用成功连接到数据库

telnet postgreDB 5432

所以看起来它是可见的,但应用仍然无法连接到它。

将数据源 url 属性从 postgreDB 更改为类似 localhost

spring.datasource.url=jdbc:postgresql://localhost:5432/stuffer

让我从 docker 外部将应用程序连接到 postgre,但我需要它们都在 docker 中运行。

【问题讨论】:

【参考方案1】:

尝试将您的数据源设置为jdbc:postgresql://postgreDB:5432/stuffer。 docker-compose 根据提供的别名为每个容器分配一个主机名,或者如果没有指定别名,它使用在 docker-compose.yml 中分配的服务名称。这一切都在 docker 主机管理的网络中。

【讨论】:

【参考方案2】:

当您使用 docker 仅运行 postgresDB 时,假设您已经转发了端口,postgres 在您的主机操作系统上可用,您可以使用 localhost:5432 访问该服务

但是当您使用 docker compose 运行时,每个容器都是不同的,因此您无法从 spring 应用程序容器访问localhost:5432。在这种情况下,postgresDB 在单独的容器中运行,您需要使用容器的 IP 地址访问它。

但是,当您配置了服务之间的依赖关系时,docker compose 会为您解决这个问题,docker compose 会在您的容器中添加一个主机条目,这意味着您现在可以使用 spring 应用程序容器中的postgresDB:5432 访问 postgresDB。

查看您的 application-dev.properties 配置看起来是正确的,但包含 localhost 的数据源 url 在 docker compose 的情况下将不起作用。

【讨论】:

是的,我知道这一点。我在我的配置中使用了 postgreDB,但仍然出现错误。我只提到 localhost 作为我在 docker 之外连接数据库的唯一方式。【参考方案3】:

我设法通过运行解决了问题

docker-compose build 

运行前

docker-compose up 

问题是有一个旧图像通过 localhost 指向 postgre。尽管我将配置中的主机更改为 postgreDB,但它并不是在 up 命令期间构建的,正如我从命令手册中所想的那样。 所以结果没有反映任何变化。

【讨论】:

以上是关于无法将 dockerized spring boot 应用程序连接到 dockerized postgresql的主要内容,如果未能解决你的问题,请参考以下文章

spring boo的简单搭建(eclipse+springboot + redis + mysql + thymeleaf)

使用 GraphQL Java 工具时 Spring Boot 应用程序无法启动

Spring Boot 1.4 QueryDSL 依赖问题

如何在 Spring Boot 1.4 中自定义 Jackson

SpringBoot运行原理

spring 源码