警告:未设置 DB_USER 变量

WARNING: The DB_USER variable is not set

我正在为我的项目使用 docker compose。 docker-compose up --build:

之后我有一个奇怪的错误

WARNING: The DB_USER variable is not set. Defaulting to a blank string.

我该如何解决这个错误? (我同时尝试了 ./.env 和 .env)怎么了?

项目结构

.
├── docker-compose.yml
├── project
   |---Dockerfile
|__.env

.env

DB_USER=postgres
DB_PASSWORD=post222
DB_NAME=edo_db
DB_PORT=5444

DATABASE_URL=postgres://postgres:post222@db:5432/edo_db"
DEBUG=1

docker-compose.yml

version: '3.9'

services:
  django:
    build: ./project # path to Dockerfile
    command: sh -c "
      python manage.py makemigrations
      && python manage.py migrate  
      && gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
    volumes:
      - ./project:/project
      - ./project/static:/project/static
    expose:
      - 8000
    env_file:
      - ./.env
  
  db:
    image: postgres:13-alpine
    volumes:
      - pg_data:/var/lib/postgresql/data/
    expose: 
      - 5432
    env_file:
      - ./.env
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
  
  nginx:
    image: nginx:1.19.8-alpine
    depends_on: 
      - django
    env_file:
      - ./.env
    ports: 
      - "80:80"
    volumes:
      - ./project/static:/var/www/html/static
      - ./project/nginx-conf.d/:/etc/nginx/conf.d
  
volumes:
    pg_data:
    static:
echo $DB_USER  #  see whether this variable is defined or not prior to below

要使用 .env 文件中的变量,您必须获取 .env 文件

source .env    # good

source ./.env  # also good ... same as above yet safer

. .env  #  also good since . is same command source

. ./.env  #  good too also sources file .env

.env  #  BAD - this just executes the file which happens in a subshell and has NO impact up on parent shell ( in the terminal you executed it from ) 

现在在您获得源文件后确认变量现在已定义

echo $DB_USER

现在做你的docker-compose up

错误原因是 .env 中的拼写错误 文件:已替换

DATABASE_URL=postgres://postgres:post222@db:5432/edo_db"

DATABASE_URL=postgres://postgres:post222@db:5432/edo_db