postgresql 使用docker-compose对用户postgres进行密码验证失败

yqkkidmi  于 2022-11-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(217)

我尝试从一个节点bash向一个带有数据库的节点后端发出请求,所以首先我启动这个docker-compose:

version: '3.9'

services:
  backend:
    build: .
    ports:
       - '2000:2000'
  db:
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: '12345'
      POSTGRES_DB: 'attendancebd'

它运行得很好。我只是创建了一个bash in节点,当我做一个curl时,像这样:

curl backend:2000/info/teachers

它会给我数据库中正确的JSON。
但当我用这段文字整理时:

version: '3.9'

services:
  server:
    build: .
    ports:
      - '5000:5000'
    networks:
      serveranddb:
        aliases:
          - server_host
      serverandbash:
        aliases:
          - server_host          
  db:
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: '12345'
      POSTGRES_DB: 'attendancebd'
    networks:
      serveranddb:
        aliases:
          - db_host          
  mybash:
    build: ./bash
    stdin_open: true
    tty: true
    networks:
      serverandbash:
        aliases:
          - bash_host    
networks:
  serveranddb:
    name: server_db
    driver: bridge
    ipam:
      driver: default
  serverandbash:
    name: server_bash
    driver: bridge
    ipam:
      driver: default

我用bash中的同样的方法卷起来:

curl server_host:2000/info/teachers

它向我传达了一个信息:

{"message":"password authentication failed for user \"postgres\""}

而且环境都是一样的,密码和数据库都是一样的,为什么会发生这种情况呢?

11dmarpk

11dmarpk1#

我找到了解决方案,我必须将depends_on放入服务器服务中,这样它将等待数据库启动:

server:
    build: .
    ports:
      - '5000:5000'
    networks:
      serveranddb:
        aliases:
          - server_host
      serverandbash:
        aliases:
          - server_host
    depends_on:
      - db

相关问题