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:
尝试更新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 forscript:start
likeNODE_ENV=test node server.js
,then this overrules any setting in yourdocker-compose.yml
file.