0
votes

I have a Spring boot app that is containerized and published to the docker hub. I have this docker-copmse.yml:-

version: '3.1'
services:

postgres:
  image: postgres
  container_name: postgres
  ports:
    - "5432:5432"
  environment:
    - POSTGRES_PASSWORD=postgres
    - POSTGRES_USER=postgres
    - POSTGRES_DB=mydb

profile_back:
  container_name: profile_back
  image: madsum/profile_back
  ports:
    - "8080:8080"
  depends_on:
    - postgres

My spring boot application.propertiese:-

#spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.url=jdbc:postgresql://postgres:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true

I don't know what is the correct spring.datasource.url. I tried both jdbc:postgresql://localhost:5432/mydb and jdbc:postgresql://postgres:5432/mydb .Both give connection error. Btw, container runs perfectly. I can verify by it as docker exec -it --user postgres dbpostgresql sh . I can see the database created. What is the correct way to connect it?

Updated question:-

I am using flyway. Here is the exception now:-

ound 1 JPA repository interfaces. 2020-03-06 09:33:01.939 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-03-06 09:33:01.949 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-03-06 09:33:01.949 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31] 2020-03-06 09:33:01.997 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-03-06 09:33:01.998 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1180 ms 2020-03-06 09:33:02.118 INFO 1 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate 2020-03-06 09:33:02.124 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-03-06 09:33:02.127 WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2020-03-06 09:33:02.130 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2020-03-06 09:33:02.141 INFO 1 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-03-06 09:33:02.145 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

2
I have added an answer below but if that does not work please post more details like what is the spring boot version, and the exception you were getting when connecting to DB and any other details you think might help.Brooklyn99

2 Answers

0
votes

Carefully reading the exception, gives you a valuable hint:

Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE 2020-03-06

So it seems your application tries to connect to an in-memory H2 database and not to PostgreSQL.

Do you maybe use different profiles for your application and forgot to set the production profile? Otherwise, you can get rid of the H2 dependency in your pom.xml/build.gradle if you don't need it

0
votes

Try using 127.0.0.1 instead of localhost