Docker Compose + Spring Boot + Postgres连接,导致错误

时间:2018-11-16 05:10:44

标签: java spring postgresql spring-boot docker

我有一个Java Spring Boot应用程序,可与Postgres数据库一起使用。我想对他们两个都使用Docker。最初,我创建了一个docker-compose.yml文件,如下所示:

version: '3'
services:
  db:
    container_name: sample_db
    image: postgres
    volumes:
      - sample_db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB="employee"
      - POSTGRES_USER="user"
      - POSTGRES_PASSWORD="password"
      - PGDATA=/var/lib/postgresql/data/pgdata

  web:
    build: .
    expose:
      - "8080"
    depends_on:
      - db
volumes:
  sample_db: {}

然后,在我的Spring Boot应用程序的application.properties文件中,定义了以下属性。

spring.datasource.url=jdbc:postgresql://db:5432/employee
spring.datasource.username=user
spring.datasource.password=password

与此同时,我在项目目录中创建了一个Dockerfile,如下所示:

FROM openjdk:8-jdk-alpine
EXPOSE 8080    
ARG JAR_FILE=target/employee-0.0.1-SNAPSHOT.jar    
COPY ${JAR_FILE} employee-demo.jar    
ENTRYPOINT ["java","-jar","employee-demo.jar"]

我发出了这些命令,最终出现如下错误

  

MVN清洁包

     

docker-compose up

问题是连接字符串。我相信我必须将其更改为其他内容,但我不知道应该是什么。我收到以下错误消息:

 Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
web_1  |    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar!/:na]
web_1  |    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1  |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1  |    at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
web_1  |    ... 44 common frames omitted
web_1  | Caused by: java.net.UnknownHostException: sample_db
web_1  |    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_181]
web_1  |    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
web_1  |    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
web_1  |    at org.postgresql.core.PGStream.<init>(PGStream.java:70) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.5.jar!/:42.2.5]
web_1  |    ... 58 common frames omitted

是docker的新手,如果有任何问题,请告知

2 个答案:

答案 0 :(得分:1)

您的服务名称为db,这是您需要从Web容器中引用为主机名的名称。无法从Web容器解析容器名称sample_db,因为您尚未指定网络,因此使用的是默认网桥网络。

根据文档,默认桥接网络不提供容器之间的名称解析,仅提供IP。使用docker compose,您应该能够使用服务名称进行解析。

https://docs.docker.com/network/bridge/

https://docs.docker.com/compose/networking/

version: '3'
services:
  db:
    container_name: sample_db
    image: postgres
    volumes:
      - sample_db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB="employee"
      - POSTGRES_USER="user"
      - POSTGRES_PASSWORD="password"
      - PGDATA=/var/lib/postgresql/data/pgdata
    networks:
      - my_network
  web:
    build: .
    expose:
      - "8080"
    depends_on:
      - db
    networks:
      - my_network
volumes:
  sample_db: {}
networks:
  my_network:
    driver: bridge

链接也应该起作用,而不是定义自己的自定义网桥:

  web:
    build: .
    expose:
      - "8080"
    depends_on:
      - db
    links:
      - "db:sample_db"

答案 1 :(得分:0)

这是因为" "的错误输入。我将其更改为

version: '3'
services:
  db:
    restart: always
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=employee
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password

  web:
    build: .
    ports:
      - 8080:8080

现在一切正常。谢谢您的宝贵时间。