1

我正在使用 Spring、Postgres 和 docker-compose。我想为数据库用户和密码使用秘密,但它不起作用:

version: '3.5'

services:
  web:
    build:
      context: hello-todos-server
    container_name: hello-todos-server
    secrets:
      - db-password
      - db-user
    depends_on:
      - hello-todo-db
    environment:
      SPRING_DATASOURCE_USERNAME_FILE: /run/secrets/db-user
      SPRING_DATASOURCE_PASSWORD_FILE: /run/secrets/db-password

    networks:
      - traefik
      - hello-todos
    labels:
      - 'traefik...'
      - 'traefik...`)'
      - 'traefik...'
      - 'traefik...'

  hello-todo-db:
    image: postgres:14.1-alpine3.15
    restart: always
    container_name: hello-todo-db
    environment:
      POSTGRES_USER_FILE: /run/secrets/db-user
      POSTGRES_PASSWORD_FILE: /run/secrets/db-password
      POSTGRES_DB: hello-todos
    secrets:
      - db-password
      - db-user
    networks:
      - hello-todos
secrets:
  db-password:
    file: secrets/secret-db-password.txt
  db-user:
    file: secrets/secret-db-user.txt

networks:
  traefik:
    external: true
  hello-todos:
    external: false

错误:

Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"

...

当我通过连接到数据库时

environment:
      SPRING_DATASOURCE_USERNAME: the-plain-username-stored-in-the-secret
      SPRING_DATASOURCE_PASSWORD: the-plain-password-stored-in-the-secret

它确实有效。但我想这样做

environment:
      SPRING_DATASOURCE_USERNAME_FILE: /run/secrets/db-user
      SPRING_DATASOURCE_PASSWORD_FILE: /run/secrets/db-password

这可能吗?还是有更好的解决方案,也许是通过 application.yml?

我开始我的服务docker-compose up

我的应用程序.yml:

spring:
  datasource:
    url: jdbc:postgresql://hello-todo-db:5432/hello-todos
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    show-sql: true
    database-platform: org.hibernate.dialect.PostgreSQLDialect
4

0 回答 0