简体   繁体   English

无法将 PostgreSQL db 连接到 Spring 与 docker

[英]Can't connect PostgreSQL db to Spring with docker

I am having issues connecting my Spring app to db with docker.我在使用 docker 将我的 Spring 应用程序连接到数据库时遇到问题。 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 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 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.尝试使用 postgres 服务的 container_name 而不是 localhost。

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

Here is a post explaining why:这是一个解释原因的帖子:

docker-compose.yml container_name and hostname docker-compose.yml 容器名和主机名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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