1

I am using docker compose for my project. And I have a strange error after docker-compose up --build:

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

How can I fix this error? (I was trying both ./.env and .env) What is wrong?

Project structure

.
├── 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:
Alex
  • 562
  • 1
  • 6
  • 25

3 Answers3

2
echo $DB_USER  #  see whether this variable is defined or not prior to below

to engage the variables in file .env you must source the .env file

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 ) 

now after you have source the file confirm the variable is now defined

echo $DB_USER

now do your docker-compose up

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
  • BTW, note that `echo $variable` [isn't all that reliable](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). `declare -p variable` is generally preferable if the shell is known to be bash (as opposed to `/bin/sh`). If one needs POSIX compatibility, `printf '%s\n' "$variable"` is a better choice -- more consistent handling of backslashes and other corner cases across implementations than `echo "$variable"`, which is itself better than `echo $variable` for reasons given in the prior link. – Charles Duffy May 12 '22 at 00:45
  • compose reads .env files automatically, so this is at least a incomplete answer, assuming you refering to doing this inside the django container. Its its regarding django, there are better solutions to doing this, i.e. using os.environ oder some dotenv package. – The Fool May 13 '22 at 18:51
1

The reason of the error was typo in .env file: Replased

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

with

DATABASE_URL=postgres://postgres:post222@db:5432/edo_db
Alex
  • 562
  • 1
  • 6
  • 25
1

Just in case anybody lands here. I had a similar problem where my environment variables for Postgres were not being read by the compose file.

My initial project structure was as follows:

.
├── api/
│   ├── a-django-app/
│   ├── Dockerfile
│   └── .env.dev
├── frontend/
│   └── <some React files>
└── docker-compose-yml

and the docker-compose.yml file was as follows:

version: '3.9'

services:
    api:
      --other attributes here--
      env_file:
        - ./api/.env.dev
      depends_on:
        - postgres

    postgres:
      ---other attributes here---
      environment:
        - POSTGRES_DB=${DB_NAME}
        - POSTGRES_PASSWORD=${DB_PASSWORD}
        - POSTGRES_USER=${DB_USER}

What solved this problem for me was making the following changes:

  1. Renamed .env.dev to .env;
  2. Moved the .env file to the project's root directory so that it's on the same level as the docker-compose.yml file;
  3. Updated my docker-compose.yml file env_file attribute to .env;

My new project structure changed to this:

.
├── api/
│   ├── a-django-app/
│   └── Dockerfile
├── frontend/
│   └── <some React files>
├── .env
└── docker-compose-yml

and my docker-compose.yml file changed to this:

version: '3.9'

services:
    api:
      --other attributes here--
      env_file:
        - .env
      depends_on:
        - postgres

    postgres:
      ---other attributes here---
      environment:
        - POSTGRES_DB=${DB_NAME}
        - POSTGRES_PASSWORD=${DB_PASSWORD}
        - POSTGRES_USER=${DB_USER}

I hope this helps someone who is facing a similar problem.

Drakmord2
  • 864
  • 6
  • 13