7

I'm running a Docker container with Django inside.

Here is my dev.yml file:

version: '2'

volumes:
  postgres_data_dev: {}
  postgres_backup_dev: {}

services:
  postgres:
    build: ./compose/postgres
    volumes:
      - postgres_data_dev:/var/lib/postgresql/data
      - postgres_backup_dev:/backups
    environment:
      - POSTGRES_USER=sorbetcitron

  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile-dev
    command: python ./manage.py runserver_plus 0.0.0.0:8000
    depends_on:
      - postgres
    environment:
      - POSTGRES_USER=sorbetcitron
      - USE_DOCKER=yes
      - DJANGO_DEBUG=True
      - DATABASE_URL=postgres://django:django@localhost:5432/sorbetcitron
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    links:
      - postgres



  pycharm:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile-dev
    depends_on:
      - postgres
    environment:
      - POSTGRES_USER=sorbetcitron
    volumes:
      - .:/app
    links:
      - postgres

Then I start my container with:

docker-compose -f dev.yml up

But I got not environment variable set up when I:

docker-compose -f dev.yml run django echo $DATABASE_URL

I just want to test a simple switch between the database in my container and another in my local machine.

Edit#1 My variable is effectively set, as docker-compose config returns:

services:
  django:
    build:
      context: /Users/vincentle/dev/sorbetcitron
      dockerfile: ./compose/django/Dockerfile
    command: /gunicorn.sh
    depends_on:
    - postgres
    - redis
    environment:
      DATABASE_URL: postgres://django:django@localhost:5432/sorbetcitron
      DJANGO_ACCOUNT_ALLOW_REGISTRATION: 'True'
      DJANGO_ADMIN_URL: ''
      DJANGO_ALLOWED_HOSTS: .sorbetcitron.com
      DJANGO_AWS_ACCESS_KEY_ID: ''
      DJANGO_AWS_SECRET_ACCESS_KEY: ''
      DJANGO_AWS_STORAGE_BUCKET_NAME: ''
      DJANGO_MAILGUN_API_KEY: ''
      DJANGO_SECRET_KEY: s+s6-^@s&=xg@l7!qsprhd5-1-0*wuh*0tjm_5)%uq(5q(nc4c
      DJANGO_SECURE_SSL_REDIRECT: 'False'
      DJANGO_SENTRY_DSN: ''
      DJANGO_SERVER_EMAIL: ''
      DJANGO_SETTINGS_MODULE: config.settings.production
      POSTGRES_PASSWORD: mysecretpass
      POSTGRES_USER: postgresuser
    user: django

But I don't understand why my Django is not taking this variable in account (I can play with data stored in the docker's postgres db)

my config.py file:

DATABASES = {
    # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ
    'default': env.db('DATABASE_URL', default='postgres:///sorbetcitron'),
}

2 Answers 2

7

Using:

env.db('DATABASE_URL', default='postgres:///sorbetcitron')

means that you're using django-environ package to set environment variables, in that case you need a .env file with all your used variables. If you're not using this package, that's why are not getting loaded on django settings. In that case you should use:

import os
os.getenv('DATABASE_URL')

or

import os
os.environ.get('DATABASE_URL')
Sign up to request clarification or add additional context in comments.

Comments

1

I am not a Python expert but your code to read the Environment variables looks wrong to me.

Please try:

import os
DB_URL = os.getenv('DATABASE_URL')

1 Comment

@ArdiNusawan yes that would work too, but getenv (or even os.environ.get('DATABASE_URL') works better because it will return none if DATABASE_URL Is not set. where as os.environ['DATABASE_URL'] will return a KeyError if not set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.