title: Why does .env file isn't working in django docker?
body: <p>I am working on django docker project. I added .env file, but I have this error:</p>
  <pre><code>celery_1  |   File &quot;/project/core_app/__init__.py&quot;, line 5, in &lt;module&gt;
  celery_1  |     from .celery import app as celery_app
  celery_1  |   File &quot;/project/core_app/celery.py&quot;, line 11, in &lt;module&gt;
  celery_1  |     from core_app.settings import INSTALLED_APPS
  celery_1  |   File &quot;/project/core_app/settings.py&quot;, line 90, in &lt;module&gt;
  celery_1  |     'NAME': os.environ['DB_NAME'],
  celery_1  |   File &quot;/usr/local/lib/python3.9/os.py&quot;, line 679, in __getitem__
  celery_1  |     raise KeyError(key) from None
  celery_1  | KeyError: 'DB_NAME'
  </code></pre>
  <p>It seems that I specified <code>env_file</code> in docker-compose correctly but error said that code doesn't see environment variables.</p>
  <p>How can I fix the problem?</p>
  <p><strong>settings.py</strong></p>
  <pre><code>import environ
  import os
  
  env = environ.Env()
  
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': os.environ['DB_NAME'],
          'USER': os.environ['DB_USER'],
          'PASSWORD': os.environ['DB_PASSWORD'],
          'HOST': 'db',
          'PORT': 5432,
      }
  }
  </code></pre>
  <p><strong>.env</strong> -- just a few configs</p>
  <pre><code>DB_USER=postgres
  DB_PASSWORD=post222
  DB_NAME=lk_potok_4
  DB_PORT=5444
  
  DATABASE_URL=postgres://postgres:post222@db:5432/lk_potok_4&quot;
  </code></pre>
  <p><strong>docker-compose.yml</strong> -- full file <a href="https://pastebin.com/0feAg5xq" rel="nofollow noreferrer">https://pastebin.com/0feAg5xq</a></p>
  <pre><code>version: '3.9'
  
  services:
    django:
      build: ./project # path to Dockerfile
      command: sh -c &quot;
        python manage.py makemigrations
        &amp;&amp; python manage.py migrate  
        &amp;&amp; gunicorn --bind 0.0.0.0:8000 core_app.wsgi&quot;
      volumes:
        - ./project:/project
        - ./project/static:/project/static
      expose:
        - 8000
      env_file:
        - .env
      environment:
        - DATABASE_URL=${DATABASE_URL}&quot;
        - DEBUG=1
        - DB_USER=${DB_USER}
        - DB_PASSWORD=${DB_PASSWORD}
        - DB_NAME=${DB_NAME}
        
    
    db:
      image: postgres:13-alpine
      volumes:
        - pg_data:/var/lib/postgresql/data/
      expose: 
        - 5432
      environment: 
        - DB_USER=${DB_USER}
        - DB_PASSWORD=${DB_PASSWORD}
        - DB_NAME=${DB_NAME}
    
    nginx:
      ...
    
    celery:
      build: ./project
      command: celery -A core_app worker  --loglevel=info
      volumes:
        - ./project:/usr/src/app
      environment:
        - DEBUG=1
        - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
        - CELERY_BROKER=redis://redis:6379/0
        - CELERY_BACKEND=redis://redis:6379/0
      depends_on:
        - django
        - redis
  
    ...
  </code></pre>