Maven在尝试建立与数据库的连接的Spring项目构建中失败

时间:2019-03-18 16:37:51

标签: java maven spring-boot docker

我为我的spring项目创建了一个docker-compose和dockerfile,但是在maven构建阶段失败了,因为尝试与postgres服务建立连接,我不知道该如何解决。

这是我的docker-compose文件:

version: '3'
services:

  postgres:
    build:
      context: .
      dockerfile: docker/postgres/Dockerfile
    restart: always
    environment:
      POSTGRES_DB: ${postgres_database}
      POSTGRES_USER: ${postgres_username}
      POSTGRES_PASSWORD: ${postgres_password}
    ports:
      - '5432:5432'
    volumes:
      - ./pgdata:/var/lib/postgresql/data

  api:
    build:
      context: .
      dockerfile: docker/jdk/Dockerfile
    restart: always
    environment:
      postgres_database: ${postgres_database}
      postgres_username: ${postgres_username}
      postgres_password: ${postgres_password}
    ports:
      - '8100:8080'
    depends_on:
      - postgres

我的postgres dockerfile:

FROM postgres:11-alpine

ENV POSTGRES_DB: ${POSTGRES_DB}
ENV POSTGRES_USER: ${POSTGRES_USER}
ENV POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

COPY ./docker/postgres/init.sql /docker-entrypoint-initdb.d

EXPOSE 5432

我的jdk dockerfile,我在其中构建项目(失败)并运行jar:

FROM maven:3.6.0-jdk-11-slim AS build

COPY ./src /usr/src/app/src
COPY ./pom.xml /usr/src/app

RUN mvn -f /usr/src/app/pom.xml clean package

FROM adoptopenjdk/openjdk11:jre11u-alpine-nightly

COPY --from=build /src/usr/src/app/target/logger_service-0.0.1-SNAPSHOT.jar /usr/src/app/app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/usr/app/app.jar"]

错误日志:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 14.844 s <<< FAILURE! - in cl.getcolors.loggerservice.LoggerServiceApplicationTests
[ERROR] contextLoads(cl.getcolors.loggerservice.LoggerServiceApplicationTests)  Time elapsed: 0.014 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
Caused by: java.net.UnknownHostException: postgres

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,maven尝试执行应用程序的测试,因此它会根据配置执行对数据库的调用,这是通过mvn clean package命令执行的。 -DskipTests必须在命令中添加指令以运行无测试的maven,这样您才能正确构建。

mvn clean package -DskipTests

我的dockerfile现在看起来像这样,并且可以正常工作:

FROM maven:3.6.0-jdk-11-slim AS build

COPY ./src /usr/src/app/src
COPY ./pom.xml /usr/src/app

RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests

FROM adoptopenjdk/openjdk11:jre11u-alpine-nightly

COPY --from=build /src/usr/src/app/target/logger_service-0.0.1-SNAPSHOT.jar /usr/src/app/app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/usr/app/app.jar"]