skip to Main Content

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:

3

Answers


  1. Chosen as BEST ANSWER

    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
    

  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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search