【问题标题】:Can't connect PostgreSQL db to Spring with docker无法使用 docker 将 PostgreSQL 数据库连接到 Spring
【发布时间】:2022-01-04 02:56:19
【问题描述】:

我在使用 docker 将我的 Spring 应用程序连接到 db 时遇到问题。有人可以给我建议有什么问题吗?

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

码头工人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

码头工人撰写

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

我在尝试运行应用程序时遇到一堆错误 Errors

【问题讨论】:

    标签: java spring spring-boot docker


    【解决方案1】:

    尝试使用 postgres 服务的 container_name 而不是 localhost。

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

    这是一个解释原因的帖子:

    docker-compose.yml container_name and hostname

    【讨论】:

    • 是的,并且 container_name 并不是真正需要的。然后您可以使用postgress 而不是postgres_container
    猜你喜欢
    • 2019-07-04
    • 2018-04-08
    • 1970-01-01
    • 2022-11-16
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2018-12-24
    相关资源
    最近更新 更多