0

I have a spring app in a docker container which should connect to postgresql DB on host

docker-compose.yml

version: '3.8'

services:
  spring-app-container:
    image: spring-app:1
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "50700:50700"
    environment:
      - DATABASE_HOST=192.168.65.2
      - DATABASE_USER=user
      - DATABASE_PASSWORD=****
      - DATABASE_NAME=db_name
      - DATABASE_PORT=5432

From the docker container I can ping the DB host IP and I can telnet 192.168.65.2 5432.

postgres configurations are:

pg_hba.conf

# IPv4 local connections:
host    all             all             0.0.0.0/0            scram-sha-256

and

postgresql.conf

listen_addresses = '*'      # what IP address(es) to listen on;

Now when I start docker-compose up, it says:

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.

Why does it say localhost here? In the docker-compose.yml the DB host is set to DATABASE_HOST=192.168.65.2

I also have DB settings in application.properties.

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/db_name
spring.datasource.username=user
spring.datasource.password=****

Will this be overwritten or do I need to set this to 192.168.65.2 instead of localhost as well?

Am I missing something else here?

midi
  • 3,128
  • 5
  • 30
  • 47
  • 1
    You'll need to replace the IP (192.168.65.2) in place of _localhost_ in the application.properties file. Since you are passing DB config values as env variables in the Docker compose file, then why are you not using them in the application.properties file? Strange!!! – Kapil Khandelwal Jun 14 '22 at 18:29
  • This did the trick. Passing these values from the docker-compose.yml to application.properties made it work. Thank you very much. – midi Jun 15 '22 at 08:22

0 Answers0