我正在尝试遵循Docker文档和SO建议,但是在尝试“ docker-compose up db”时仍然出现“ variable not set”错误。有人可以检查我的.env和yaml文件吗?两者都在同一个目录中,但是yml似乎没有读取.env ???
.env:
FS_NAME=MY_BLOG_NAME
FS_REGION=us-east-2
DB_HOST=localhost
DB_USER=postgres
DB_PASS=$$323MY_PASS$&84
DB_NAME=postgres
docker-compose.yml:
version: '3.7'
volumes:
db_data:
x-shared_environment: &shared_environment
LOG_LEVEL: ${LOG_LEVEL:-debug}
DATABASE_HOST: ${DB_HOST}
DATABASE_NAME: ${DB_NAME}
DATABASE_USERNAME: ${DB_USERNAME}
DATABASE_PASSWORD: ${DB_PASSWORD}
services:
app:
image: MY_BLOG_NAME:latest
build:
context: .
environment:
<<: *shared_environment
FS_NAME: ${FS_NAME}
FS_REGION: ${FS_REGION}
depends_on:
- db
ports:
- '8080:8080'
db:
image: postgres:12.2-apline
volumes:
-db_data/var/lib/postrgresql/data/pgdata
environment:
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: {DB_USERNAME}
POSTGRESS_PASSWORD: {DB_PASSWORD}
POSTGRES_DB: {DB_NAME}
POSTGRESS_PASSWORD: {DB_PASSWORD}
ports:
-'5432:5432'
答案 0 :(得分:1)
您需要指定.env
文件的目录,为每个服务添加env_file
配置。
https://docs.docker.com/compose/environment-variables/
例如:
db:
image: postgres:12.2-apline
volumes:
-db_data/var/lib/postrgresql/data/pgdata
env_file:
- ./.env
environment:
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: {DB_USERNAME}
POSTGRESS_PASSWORD: {DB_PASSWORD}
POSTGRES_DB: {DB_NAME}
POSTGRESS_PASSWORD: {DB_PASSWORD}
ports:
-'5432:5432'