Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven fail in spring project build trying to establish connection with database

i make a docker-compose and dockerfile for my spring project, but it fail on maven build stage becouse is trying to establish a connection with the postgres service, I have no idea how I could solve it.

this is my docker-compose file:

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

my 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

my jdk dockerfile where i made the project build (it fails) and run the 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 log:

[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

how can i fix it?

like image 223
kmilo93sd Avatar asked Oct 30 '25 06:10

kmilo93sd


1 Answers

I have found the solution, maven tries to execute the tests of the application so it executes the call to the database based on the configuration, this happens with the mvn clean package command. -DskipTests must be added the instruction to the command to run maven no tests, so you can build correctly.

mvn clean package -DskipTests

my dockerfile now look like this and work perfectly:

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"]
like image 173
kmilo93sd Avatar answered Oct 31 '25 21:10

kmilo93sd