pythondjangodockerdocker-composeenvironment-variables

Why aren't the environment variables working inside my Django Docker container?


I'm encountering an issue where Django doesn't seem to be reading environment variables when running in a Docker-compose environment. Strangely, the environment variables work fine for PostgreSQL but not for Django. I've tried both the env_file and environment options in my Docker-compose file, and I've also experimented with django-dotenv without success.

Here's a snippet of my code:

print(f"
{os.environ.get('SECRET_KEY')}
{os.environ.get('DEBUG')}
{os.environ.get('DATABASE_NAME')} 
")

# The above print statement outputs None for all environment variables

SECRET_KEY = os.environ.get("SECRET_KEY")
# ...
version: '3.9'

services:
  db:
    # ... 

  web:
    build: ./src
    command: gunicorn myapp.wsgi:application --access-logfile /logs/gunicorn/access.log --error-logfile /logs/gunicorn/error.log --bind 0.0.0.0:8000
    
    volumes:
      # ...
     
    # env_file:  #not working
    #   - .env
    environment:
      - SECRET_KEY=${SECRET_KEY}
      - DATABASE_NAME=${DATABASE_NAME}
      # ... (other environment variables)

  nginx:
    # ...

SECRET_KEY=3df-81ioo^5cx(p9cl$)s%m3mlu3t7*yh#1lr5h0po4_sab3*5
DATABASE_NAME=mydb
DATABASE_USER=postgres
DATABASE_PASS=postgres
DATABASE_HOST=db
DATABASE_PORT=5432
DEBUG=0

Solution

  • I believe I've identified the root cause of the issue in my Dockerfile. The problem lies in the sequence of commands during the Docker image build process. Specifically, I observed that the Dockerfile attempts to run python manage.py collectstatic --noinput and python manage.py migrate before Docker Compose copies the environment variables. This results in errors during the build process.

    To address this, I propose a solution: remove the collectstatic and migrate commands from the Dockerfile and instead include them in the Docker Compose configuration. Adjust the command parameter in the docker-compose.yml (in web part):

    
    command: "sh -c 'python manage.py migrate && python manage.py collectstatic --noinput && gunicorn -c conf/gunicorn.py techgeeks.wsgi:application'"
    
    

    Here is the previous version of my Dockerfile:

    # set work directory
    WORKDIR /app
    
    # set environment variables
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1
    
    # install dependencies
    # RUN pip install --upgrade pip
    COPY . .
    
    RUN pip install --upgrade pip
    RUN pip install -r requirements.txt
    
    # The following two lines caused issues, so they were removed
    RUN python manage.py collectstatic --noinput 
    RUN python manage.py migrate