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

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 ComposeVariable 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.

相关文章

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Li...
1、什么是docker?答:docker是开源的应用容器引擎;开发人员把他们的应用及依赖包打包发布到容器当中。...
1、什么是namespace? 答:名称空间,作用隔离容器 2、namespace隔离有那些? 答:ipc:共享内存、消息队...
1、Docker能在非Linux平台(Windows+MacOS)上运行吗? 答:可以 2 、如何将一台宿主机的docker环境...
环境要求: IP hostname 192.168.1.1 node1 项目规划: 容器网段:172.16.10.0/24 NGINX:172.16.10.10...
文档上传地址:https://files.cnblogs.com/files/lin-strive/07-docker%E8%B7%A8%E4%B8%BB%E6%9C%BA%E7...