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_SERVERvariable and replacing withlocalhost:5432,db,db:5432. None of the seems to work. - Adding depends on field in the
flamupservice. - 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.