0

Trying to start up a Spring Boot application that connects to two PostgreSQL databases using docker-compose and I am getting a connection refused when Spring tries to connect to either of these databases.

My configuration is as follows:

docker-compose.yml

version: '3.2'
services:
  mydb-1:
    container_name: mydb-1
    image: mydb-1
    ports:
      - '5432:5432'
    environment:
         - POSTGRES_PASSWORD=postgres
         - POSTGRES_USER=postgres
         - POSTGRES_DB=testdb1
  mydb-2:
    container_name: mydb-2
    image: mydb-2
    ports:
      - '5433:5432'
    environment:
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_DB=testdb2
  my-server:
    image: my-server-spring
    restart: on-failure
    depends_on:
      - mydb-1
      - mydb-2
    environment:
      - SPRING_DB1-DATASOURCE_JDBC-URL=jdbc:postgresql://mydb-1:5432/testdb1
      - SPRING_DB2-DATASOURCE_JDBC-URL=jdbc:postgresql://mydb-2:5433/testdb2
    expose:
      - '8080'
    ports:
      - '8080:8080'

Spring application.properties

spring.db1-datasource.jdbc-url= jdbc:postgresql://localhost:5432/testdb1
spring.db1-datasource.username= postgres
spring.db1-datasource.password= postgres
spring.db1-datasource.driverClassName= org.postgresql.Driver

spring.db2-datasource.jdbc-url= jdbc:postgresql://localhost:5433/testdb2
spring.db2-datasource.username= postgres
spring.db2-datasource.password= postgres
spring.db2-datasource.driverClassName= org.postgresql.Driver

Stacktrace (Part of)

org.postgresql.util.PSQLException: Connection to testdb2:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

advidi-server_1         |       at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1         |       at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1         |       at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1         |       at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1         |       at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1         |       at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1         |       at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.18.Final.jar!/:5.4.18.Final]
advidi-server_1         |       at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.18.Final.jar!/:5.4.18.Final]

Initially I thought that the problem was that the Spring application was trying to connect to the dbs before their bootstrapping is complete, but that doesn't seem to be the case since with restart: on-failure it should at some point manage to connect.

The default localhost values should also not be a problem since these are replaced by the environment variables in my docker-compose file.

Any ideas?

3
  • And you happily forgot to include the actual stacktrace/error in your post. Commented Sep 14, 2020 at 7:51
  • I happily added it :) Commented Sep 14, 2020 at 7:57
  • Is your second dockerized database properly starting? Commented Sep 14, 2020 at 8:14

2 Answers 2

2

You're not showing the mydb-1 and mydb-2 image configurations, so I'm practically guessing here.

  1. No need to map the ports to the host. Remove the ports: entries.
  2. The spring container should use port 5432 in order to connect to both DB containers. Each DB container has its own IP so there's no problem with that. Don't connect to localhost because that's the container itself.
  3. No need to specify :5432 in the connection strings because that's the default.
  4. Seems like you have connection settings both in application.properties and the environment: entry in the server configuration. Which is the application using? Anyway, your connection strings should end with mydb-1/testdb1 and mydb-2/testdb2 - nothing more complicated than that.
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I didn't add the corresponding Dockerfiles because they are pretty basic. What you are suggesting seems to work but I don't really understand why. Care to elaborate why ? Also by removing the "ports" doesn't that mean that I cannot access the db outside the container?
I think that you were trying to access localhost from within the app container. Inside a container, localhost means "this container" and not "my docker host". When you specify the neighbor container names (mydb-1, mydb-2), then you are able to reach them. If you need access from the outside then you may return the ports: entries, but take into account that they are not used by the containers themselves.
0

You're passing SPRING_DB1-DATASOURCE_JDBC-URL in your environment, and not using it in the application.properties. So the variable are not overriding.

You need to either in application.properties use something like
spring.db1-datasource.jdbc-url=$SPRING_DB1-DATASOURCE_JDBC-URL
OR
in environment set the exact name of the variables in your application. properties
- spring.db1-datasource.jdbc-url=jdbc:postgresql://mydb-1:5432/testdb1

I would suggest the second option, which will still allow you to use the application.properties as default values for running locally.

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.