1

I have a docker compose file where i have two services the flamup service is used for building my spring boot application and links to a postgres image defined by the db service.

Here's the docker-compose.yml file

version: '3.8'


volumes:
  postgres_data:

services:

  flamup:
    build: .
    container_name: flamup
    environment:
      - DB_SERVER=db
      - POSTGRES_DB=flamup
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "8080:8080" # Forward the exposed port 8080 on the container to port 8080 on the host machine
    depends_on:
      - db
    links:
      - db



  db:
    image: "postgres:9.6-alpine"
    container_name: postgres_container
    restart: always

    volumes:
      - postgres_data:/var/lib/postgresql/data

    ports:
      - "5432:5432"

    environment:
      - POSTGRES_DB=flamup
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PGDATA=/var/lib/postgresql/data/pgdata

Here's the Dockerfile for the spring boot application

FROM maven:3.6.1-jdk-8-slim AS build
RUN mkdir -p workspace
WORKDIR workspace
COPY pom.xml /workspace
COPY src /workspace/src
COPY frontend /workspace/frontend
COPY data2.csv /workspace
RUN mvn -f pom.xml clean package
#RUN ./mvnw clean package -DskipTests

#FROM adoptopenjdk/openjdk11:alpine-jre
FROM openjdk:8-alpine
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

I am using the frontend-maven-plugin for bootstrapping the frontend with the spring boot application. Everything works fine locally.

However when i try to run it via docker-compose i made some changes in the application-properties.yml

server:
  error:
    include-message: always
    include-binding-errors: always


spring:
  datasource:
    url : jdbc:postgresql://${DB_SERVER}/${POSTGRES_DB}
    username : ${POSTGRES_USER}
    password : ${POSTGRES_PASSWORD}

  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
        jdbc:
          lob:
            non_contextual_creation: true
    show-sql: true

  session:
    store-type: jdbc
    jdbc:
      table-name: SPRING_SESSION
      initialize-schema: never

However the build get's failed everytime, because of this

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: ${DB_SERVER}

Things i have tried

  • Hard coding the DB_SERVER variable and replacing with localhost:5432 , db , db:5432. None of the seems to work.
  • Adding depends on field in the flamup service.
  • Adding networks field in both the services and connecting them via a bridge.
  • Tried different versions of postgres and maven

Nothing seems to be the solution, any help will be highly appreciated.

1

1 Answer 1

1

So a working solution to the problem ( though may not be that efficient ), is running the maven build by skipping the tests. Apparently hibernate results in running the JPA statements which results in failure of JDBC postgres exception as there is yet no container of postgres running.

Thus changing the dockerfile to

RUN mvn -f pom.xml clean install -DskipTests=true

instead of

RUN mvn -f pom.xml clean package

caused in successful creation of the jar file which could be run as a docker container.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.