skip to Main Content

I am very confused and looking for help. I’m trying to dockerize my Spring Boot application so I set up a simple dockerfile and docker-compose. No matter what I try, though, I keep getting the same error after I build and run my containers: "Connection to localhost:5432 refused". I already looked at a bunch of threads on the internet that said to change my spring datasource url to use the service name for my database instead of localhost (so "jdbc:postgresql://database:5432/db" instead of "jdbc:postgresql://localhost:5432/db") but after rebuilding, this appears to have done nothing to fix the problem. I can’t figure out what I’m doing wrong! Please help me.

This is my dockerfile:

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

This is my docker-compose:

services:
  backend:
    image: 'backend_festivalapp'
    container_name: 'backend_api'
    build: .
    ports:
      - '8080:8080'
    networks:
      - aad
    depends_on:
      - database
    
  database:
    image: 'postgres:latest'
    container_name: 'postgres_database'
    restart: always
    ports:
      - '${POSTGRES_PORT}:5432'
    environment:
      - 'POSTGRES_DB=${POSTGRES_DB}'
      - 'POSTGRES_USER=${POSTGRES_USERNAME}'
      - 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
    expose:
      - 5432
    networks:
      - aad
    volumes:
      - 'backend_postgres:/var/lib/postgresql/data'

networks:
  aad:
volumes:
  backend_postgres:

This is my application.properties:

spring.datasource.url=jdbc:postgresql://database:5432/${POSTGRES_DB}
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.initialization-mode=always
spring.datasource.initialize=true

logging.level.org.springframework.security=DEBUG
logging.level.org.springframework.web=DEBUG

This is my env file:

POSTGRES_PORT=5432
POSTGRES_DB=aad_backend_festivalapp
POSTGRES_USERNAME=root
POSTGRES_PASSWORD=root

This is the stack trace I get when I run ./gradlew build, then docker build ., then docker-compose up:


backend_api        | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
backend_api        |    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:342) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.jdbc.PgConnection.(PgConnection.java:263) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.Driver.makeConnection(Driver.java:443) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.Driver.connect(Driver.java:297) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:100) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-5.0.1.jar!/:na]
backend_api        |    at org.flywaydb.core.api.configuration.ClassicConfiguration.setDataSource(ClassicConfiguration.java:1094) ~[flyway-core-9.22.3.jar!/:na]
backend_api        |    at org.flywaydb.core.api.configuration.FluentConfiguration.dataSource(FluentConfiguration.java:624) ~[flyway-core-9.22.3.jar!/:na]
backend_api        |    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.configureDataSource(FlywayAutoConfiguration.java:185) ~[spring-boot-autoconfigure-3.2.3.jar!/:3.2.3]
backend_api        |    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.flyway(FlywayAutoConfiguration.java:172) ~[spring-boot-autoconfigure-3.2.3.jar!/:3.2.3]backend_api        |    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
backend_api        |    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
backend_api        |    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
backend_api        |    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
backend_api        |    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:647) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:639) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1335) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1165) ~[spring-beans-6.1.4.jar!/:6.1.4]  
backend_api        |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1231) ~[spring-context-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:949) ~[spring-context-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) ~[spring-context-6.1.4.jar!/:6.1.4]
backend_api        |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.3.jar!/:3.2.3]
backend_api        |    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.2.3.jar!/:3.2.3]
backend_api        |    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.2.3.jar!/:3.2.3]
backend_api        |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) ~[spring-boot-3.2.3.jar!/:3.2.3]
backend_api        |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) ~[spring-boot-3.2.3.jar!/:3.2.3]
backend_api        |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-3.2.3.jar!/:3.2.3]
backend_api        |    at com.aad.aad_backend_festivalapp.SpringbootApplication.main(SpringbootApplication.java:13) ~[!/:0.0.1-SNAPSHOT]
backend_api        |    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
backend_api        |    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
backend_api        |    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
backend_api        |    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
backend_api        |    at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:91) ~[app.jar:0.0.1-SNAPSHOT]
backend_api        |    at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53) ~[app.jar:0.0.1-SNAPSHOT]
backend_api        |    at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
backend_api        | Caused by: java.net.ConnectException: Connection refused
backend_api        |    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
backend_api        |    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
backend_api        |    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:547) ~[na:na]
backend_api        |    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602) ~[na:na]
backend_api        |    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
backend_api        |    at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
backend_api        |    at org.postgresql.core.PGStream.createSocket(PGStream.java:243) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.core.PGStream.(PGStream.java:98) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:132) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:258) ~[postgresql-42.6.1.jar!/:42.6.1]
backend_api        |    ... 49 common frames omitted

2

Answers


  1. Chosen as BEST ANSWER

    Update: I figured out a way to fix it by adding the following to my backend service:

    environment:
          - SPRING_DATASOURCE_URL=jdbc:postgresql://database:5432/${POSTGRES_DB}
          - SPRING_DATASOURCE_USERNAME=${POSTGRES_USERNAME}
          - SPRING_DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
          - SPRING_JPA_HIBERNATE_DDL_AUTO=update
    

    It can connect to the database now, which makes me suspect it was not actually using the application.properties file at all. Concerning, but solved for now I guess.


  2. That is most probably because your application starts faster then your database. In fact the error is connection refused which means it can find the server but there is no application there yet to take care of the requests.

    You can first start the database and then the application and it will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search