The DATABASE_URL environment variable couldn't found in shell
See original GitHub issueWhat happened?
/app # python manage.py shell_plus
Traceback (most recent call last): File “/usr/local/lib/python3.6/site-packages/environ/environ.py”, line 273, in get_value value = self.ENVIRON[var] File “/usr/local/lib/python3.6/os.py”, line 669, in getitem raise KeyError(key) from None KeyError: ‘DATABASE_URL’
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File “manage.py”, line 30, in <module> execute_from_command_line(sys.argv) File “/usr/local/lib/python3.6/site-packages/django/core/management/init.py”, line 371, in execute_from_command_line utility.execute() File “/usr/local/lib/python3.6/site-packages/django/core/management/init.py”, line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File “/usr/local/lib/python3.6/site-packages/django/core/management/init.py”, line 204, in fetch_command settings.INSTALLED_APPS File “/usr/local/lib/python3.6/site-packages/django/conf/init.py”, line 56, in getattr self._setup(name) File “/usr/local/lib/python3.6/site-packages/django/conf/init.py”, line 43, in _setup self._wrapped = Settings(settings_module) File “/usr/local/lib/python3.6/site-packages/django/conf/init.py”, line 106, in init mod = importlib.import_module(self.SETTINGS_MODULE) File “/usr/local/lib/python3.6/importlib/init.py”, line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “<frozen importlib._bootstrap>”, line 994, in _gcd_import File “<frozen importlib._bootstrap>”, line 971, in _find_and_load File “<frozen importlib._bootstrap>”, line 955, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 665, in _load_unlocked File “<frozen importlib._bootstrap_external>”, line 678, in exec_module File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “/app/config/settings/local.py”, line 1, in <module> from .base import * # noqa File “/app/config/settings/base.py”, line 41, in <module> ‘default’: env.db(‘DATABASE_URL’), File “/usr/local/lib/python3.6/site-packages/environ/environ.py”, line 204, in db_url return self.db_url_config(self.get_value(var, default=default), engine=engine) File “/usr/local/lib/python3.6/site-packages/environ/environ.py”, line 277, in get_value raise ImproperlyConfigured(error_msg) django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable
What should’ve happened instead?
I found the below command in ./compose/production/django/entrypoint that “export DATABASE_URL” is only valid for the current shell, when you start a new shell, there is no DATABASE_URL environment variable.
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
why not place this variable in ./.env/.local/.django? It always seems to be valid in the container.
Steps to reproduce
I started a project powered by latest cookiecutter-django version using docker-compose, and when I executed “docker-compose -f local.yml up -d”, the app had been successful launched, and the http://0.0.0.0:8000/ can be visited successfully. But when I executed “docker exec -it bila_django_1(my container name) sh”, and when I want to do anything like “python manage.py shell_plus”, there is a error like above.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8
Top GitHub Comments
@zmrenwu I just had the same problem and what works for me is to source the entrypoint file so that the env variables become available: Within container shell: source compose/production/django/entrypoint
After you are in
django
container runsource /entrypoint