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?