加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

postgresql-Docker没有获取Postgres环境变量

发布时间:2020-12-16 03:21:40 所属栏目:安全 来源:网络整理
导读:Docker正在使用我的.env文件中的变量,但我不断收到错误消息: Unhandled rejection SequelizeConnectionError: role eli does not exist 我希望Postgres从docker-compose.yml中设置的环境中获取变量 .env POSTGRES_PORT=5432POSTGRES_DB=elitest4POSTGRES_US

Docker正在使用我的.env文件中的变量,但我不断收到错误消息:

Unhandled rejection SequelizeConnectionError: role “eli” does not
exist

我希望Postgres从docker-compose.yml中设置的环境中获取变量

.env

POSTGRES_PORT=5432
POSTGRES_DB=elitest4
POSTGRES_USER=eli
POSTGRES_PASSWORD=

docker-compose.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: .
    depends_on:
      - database
    ports:
      - 8000:8000
    environment:
      - POSTGRES_HOST=database
    env_file:
      - .env
  database:
    image: postgres:9.6.8-alpine
    environment: # postgress should be getting these variables,not the variables set in the env file thats for localhost
      POSTGRES_PASSWORD: password
      POSTGRES_USER: user
      POSTGRES_DB: db
    volumes:
      - pgdata:/var/lib/postgresql/pgdata
    ports:
      - 8002:5432
    env_file:
      - .env
  react_client:
      build:
        context: ./client
        dockerfile: Dockerfile
      image: react_client
      working_dir: /home/node/app/client
      volumes:
        - ./:/home/node/app
      ports:
        - 8001:8001
      env_file:
        - ./client/.env
volumes:
  pgdata:
最佳答案
TL / DR

尝试更新docker-compose服务数据库环境部分,如下所示:

environment:
  POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
  POSTGRES_USER: ${POSTGRES_USER:-user}
  POSTGRES_DB: ${POSTGRES_DB:-db}

还要注意,如果您想查看每个绑定变量最终如何在Compose中求值,则可以运行以下命令以查看“有效”的compose文件:

$docker-compose配置

此命令将打印出组成文件的外观,其中所有变量替换都替换为其评估.

请参阅文档中的Environment variables in Compose和Variable substitution部分.

请密切注意本节:

When you set the same environment variable in multiple files,here’s
the priority used by Compose to choose which value to use:

06001

In the example below,we set the same environment variable on an Environment file,and the Compose file:

06002

When you run the container,the environment variable defined in the Compose file takes precedence.

06003

Having any ARG or ENV setting in a Dockerfile evaluates only if there is no Docker Compose entry for environment or env_file.

Specifics for NodeJS containers

If you have a package.json entry for script:start like NODE_ENV=test node server.js,then this overrules any setting in your docker-compose.yml file.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读