简体   繁体   中英

Can't connect PostgreSQL db to Spring with docker

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM