Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker environment variable and Django database setup

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'),
}
like image 500
bixente57 Avatar asked Aug 19 '16 13:08

bixente57


1 Answers

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')
like image 107
Kevin Ramirez Zavalza Avatar answered Sep 20 '22 05:09

Kevin Ramirez Zavalza