Docker-compose up无法验证postgres用户

时间:2018-03-21 07:59:39

标签: postgresql docker

运行docker-compose -f production.yml up最终会返回:

postgres_1      |   Connection matched pg_hba.conf line 95: "host all all all md5"
django_1        | PostgreSQL is unavailable (sleeping)...
postgres_1      | 2018-03-21 07:48:35.575 UTC [120] FATAL:  password authentication failed for user "DbUsErName"
postgres_1      | 2018-03-21 07:48:35.575 UTC [120] DETAIL:  Password does not match for user "DbUsErName".  

在我的.envs / .production / .postgres中,我的envs以这种方式布局:

# PostgreSQL
# ------------------------------------------------------------------------------
POSTGRES_DB=luup
POSTGRES_USER=DbUsErName
POSTGRES_PASSWORD=myVeryYLongPW

在上述错误之前,终端也会输出:

celeryworker_1  | /entrypoint.sh: line 13: POSTGRES_USER: parameter not set  

然而,它确实出现在compose / production / django / entrypoint.sh中的此文件中设置:

set -o errexit
set -o pipefail
set -o nounset


cmd="$@"

if [ -z "${POSTGRES_USER}" ]; then
    # the official postgres image uses 'postgres' as default user if not set explictly.
    export POSTGRES_USER=postgres
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}"

自从我开始这个项目以来,我没有触及过很多postgres配置,因为这只是试图让它在Heroku上运行起来。所以我没有进入psql

启动和运行该怎么办?

如果需要其他信息,请与我们联系。

编辑 - production.yml:

version: '2'

volumes:
  postgres_data: {}
  postgres_backup: {}
  caddy: {}

services:
  django: &django
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    depends_on:
      - postgres
      - redis
    env_file:
      - ./.envs/.production/.django
      - ./.envs/.production/.postgres
      - ./.envs/.production/.celery
    command: /gunicorn.sh

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - postgres_backup:/backups
    env_file:
      - ./.envs/.production/.postgres

  caddy:
    build:
      context: .
      dockerfile: ./compose/production/caddy/Dockerfile
    depends_on:
      - django
    volumes:
      - caddy:/root/.caddy
    env_file:
      - ./.envs/.production/.caddy
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"

  redis:
    image: redis:3.0

  celeryworker:
    <<: *django
    depends_on:
     - postgres
     - redis
    env_file:
      - ./.envs/.production/.celery
    command: /start-celeryworker.sh

  celerybeat:
    <<: *django
    depends_on:
      - postgres
      - redis
    env_file:
      - ./.envs/.production/.celery
    command: /start-celerybeat.sh

1 个答案:

答案 0 :(得分:1)

Postgres容器在第一次运行时从env变量创建用户。因此,为了使用新值(生产用户名和密码),您可能需要重建它。

  • docker-compose stop postgres
  • docker-compose rm postgres
  • docker-compose -f production.yml up