-1

I am having issues connecting my Spring app to db with docker. Can someone give me advice what could be wrong?

spring.datasource.url=jdbc:postgresql://localhost:5433/postgres
spring.datasource.username=postgres
spring.datasource.password=changeme

spring.datasource.hikari.connection-timeout=20000
spring.datasource.hikari.maximum-pool-size=20

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

docker ps

7798a7328c2b postgres "docker-entrypoint.s…" 3 days ago Up 5 hours 5432/tcp, 0.0.0.0:5433->5433/tcp, :::5433->5433/tcp postgres_container

docker-compose

services:
  postgres:
    container_name: postgres_container
    image: postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
      PGDATA: /data/postgres
    volumes:
       - postgres:/data/postgres
    ports:
      - "5433:5433"
    networks:
      - postgres
    restart: unless-stopped

I am getting bunch of errors when trying to run the app Errors

Dave
  • 1
  • 1
  • 9
  • 38

1 Answers1

1

Try using your container_name of the postgres service instead of localhost.

spring.datasource.url=jdbc:postgresql://postgres_container:5433/postgres

Here is a post explaining why:

docker-compose.yml container_name and hostname

Neoministein
  • 11
  • 1
  • 1