0

This is my Dockerfile:

FROM openjdk:24
ARG APP_JAR=*.jar
COPY ${APP_JAR} app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
EXPOSE 8080

This is docker-compose:

version: '3.1'

services:
  app:
    image: '2fc4'
    container_name: app
    expose:
      - '8080'
    ports:
      - '8080:8080'
    environment:
      - DB_USER=postgres
      - DB_PASSWORD=2042
      - DB_URL=jdbc:jdbc:postgresql://localhost:5433/rest
    depends_on:
      - db
  db:
    image: 'postgres'
    container_name: db
    expose:
      - '5433'
    ports:
      - '5433:5433'
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=2042

And application.properties:

spring.application.name=RestAPI

#spring.datasource.url=jdbc:postgresql://localhost:5433/rest
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
spring.datasource.url=${DB_URL}

spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

server.port=8080

I try docker-compose up:

app  | 
app  |   .   ____          _            __ _ _
app  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \                                                                                                                                                                                     
app  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \                                                                                                                                                                                    
app  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )                                                                                                                                                                                   
app  |   '  |____| .__|_| |_|_| |_\__, | / / / /                                                                                                                                                                                    
app  |  =========|_|==============|___/=/_/_/_/
app  |                                                                                                                                                                                                                              
app  |  :: Spring Boot ::                (v3.4.4)                                                                                                                                                                                   
app  |                                                                                                                                                                                                                              
app  | 2025-04-27T10:56:19.756Z  INFO 1 --- [RestAPI] [           main] com.example.RestAPI.RestApiApplication   : Starting RestApiApplication v0.0.1-SNAPSHOT using Java 24 with PID 1 (/app.jar started by root in /)             
app  | 2025-04-27T10:56:19.758Z  INFO 1 --- [RestAPI] [           main] com.example.RestAPI.RestApiApplication   : No active profile set, falling back to 1 default profile: "default"
app  | 2025-04-27T10:56:20.473Z  INFO 1 --- [RestAPI] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.                                                      
app  | 2025-04-27T10:56:20.532Z  INFO 1 --- [RestAPI] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 48 ms. Found 1 JPA repository interface.
app  | 2025-04-27T10:56:20.986Z  INFO 1 --- [RestAPI] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
app  | 2025-04-27T10:56:21.000Z  INFO 1 --- [RestAPI] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
app  | 2025-04-27T10:56:21.001Z  INFO 1 --- [RestAPI] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.39]                                                                 
app  | 2025-04-27T10:56:21.026Z  INFO 1 --- [RestAPI] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
app  | 2025-04-27T10:56:21.027Z  INFO 1 --- [RestAPI] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1226 ms
app  | 2025-04-27T10:56:21.079Z  WARN 1 --- [RestAPI] [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'dat
aSourceScriptDatabaseInitializer' of LoadTimeWeaverAware bean 'entityManagerFactory': Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql
/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [or
g/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine suitable jdbc url
app  | 2025-04-27T10:56:21.082Z  INFO 1 --- [RestAPI] [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
app  | 2025-04-27T10:56:21.101Z  INFO 1 --- [RestAPI] [           main] .s.b.a.l.ConditionEvaluationReportLogger :                                                                                                                  
app  | 
app  | Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
app  | 2025-04-27T10:56:21.115Z ERROR 1 --- [RestAPI] [           main] o.s.b.d.LoggingFailureAnalysisReporter   :                                                                                                                  
app  |                                                                                                                                                                                                                              
app  | ***************************
app  | APPLICATION FAILED TO START                                                                                                                                                                                                  
app  | ***************************                                                                                                                                                                                                  
app  |                                                                                                                                                                                                                              
app  | Description:                                                                                                                                                                                                                 
app  |                                                                                                                                                                                                                              
app  | Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.                                                                                                           
app  |                                                                                                                                                                                                                              
app  | Reason: Failed to determine suitable jdbc url                                                                                                                                                                                
app  |                                                                                                                                                                                                                              
app  | 
app  | Action:                                                                                                                                                                                                                      
app  |                                                                                                                                                                                                                              
app  | Consider the following:                                                                                                                                                                                                      
app  |  If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.                                                                                                                                       
app  |  If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).                                                                                        
app  |             

The Spring Boot application should connect to the PostgreSQL database and start successfully. What could be causing the Failed to configure a DataSource error Are there any additional checks I can perform to debug this issue further?

3
  • 1
    localhost in Docker usually means "the current container"; the database is in a different container. You need to use the Compose service name db as a host name there. Consider setting a SPRING_DATASOURCE_URL environment variable directly, so you don't have to change the properties built into your jar file. You can also remove unnecessary Compose options like container_name: and expose:. Commented Apr 28 at 1:03
  • Also see for example Communications link failure , Spring Boot + MySql +Docker + Hibernate, though that doesn't quite look like the specific error you're getting – it looks like the Spring runtime isn't finding spring.datasource.url at all. The double jdbc:jdbc: at the start of your DB_URL setting could be problematic. Commented Apr 28 at 1:09
  • 1
    So, all put together, it should be DB_URL=DB_URL=jdbc:postgresql://db:5433/rest in your docker-compose.yaml Commented Apr 28 at 4:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.