Skip to content

Commit 0e59c81

Browse files
committed
Change default connection to database to use host different than docker container
- After setting up the virtual environment for the project (note missing instruction how to work locally with venv in `Readme.md`) and after running the `makemigrations --dry-run` or starting the webserver, application is trying to connect with hardcoded hostname `db` used from Dockerfile - This patch allows to add multiple hostname from environment variable `DJANGO_ALLOWED_HOSTS` separated by delimiter (I liked `,`) and since the create type in settings.py is `list` type it is free to use first value from a list if/when needed. - Add `get()` method for `os.environ` which is returning `None` instead of `KeyError` and validate parameters that we need. Using this approach only `$ export DJANGO_DB_HOST DJANGO_USER_NAME DJANGO_USER_NAME_PASSWOR` is needed. - Update `docker/.env` with new added environment variable `DJANGO_DB_HOST` - Proof * No patch ``` $ export DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1 $ ./manage.py makemigrations --dry-run /home/anel/mariadb/feedback-plugin-backend/env/lib/python3.8/site-packages/django/core/management/commands/makemigrations.py:105: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': (2005, "Unknown MySQL server host 'db' (-3)") warnings.warn( Migrations for 'feedback_plugin': feedback_plugin/migrations/0002_alter_rawdata_upload_time.py - Alter field upload_time on rawdata ``` * With patch ``` $ ./manage.py makemigrations --dry-run Migrations for 'feedback_plugin': feedback_plugin/migrations/0002_alter_rawdata_upload_time.py - Alter field upload_time on rawdata ``` - Also server gets started correctly ``` $ ./manage.py runserver Performing system checks... System check identified no issues (0 silenced). You have 19 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, feedback_plugin, sessions. Run 'python manage.py migrate' to apply them. May 30, 2022 - 14:51:50 Django version 3.2.13, using settings 'feedback_plugin.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ```
1 parent 7118c68 commit 0e59c81

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

docker/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DJANGO_DEBUG=True
2929
#
3030
# There will also be a test_{DJANGO_DB_NAME} database used for running tests.
3131
DJANGO_DB_NAME='feedback_plugin'
32+
DJANGO_DB_HOST='db'
3233
DJANGO_DB_USER_NAME='feedback'
3334
DJANGO_DB_USER_PASSWORD='A;p4rqgDt-Mf7L{z'
3435

src/feedback_plugin/settings.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,24 @@
2020
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
2121

2222
# SECURITY WARNING: keep the secret key used in production secret!
23-
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
23+
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
2424

2525
# SECURITY WARNING: don't run with debug turned on in production!
26-
DEBUG = bool(os.environ['DJANGO_DEBUG'])
27-
28-
ALLOWED_HOSTS = [os.environ['DJANGO_ALLOWED_HOSTS']]
29-
26+
DEBUG = bool(os.environ.get('DJANGO_DEBUG'))
27+
if DEBUG == None:
28+
DEBUG = True
29+
30+
if os.environ.get('DJANGO_ALLOWED_HOSTS'):
31+
ALLOWED_HOSTS = os.environ['DJANGO_ALLOWED_HOSTS'].split(',')
32+
# DB_HOST default value will be DNS on docker_default network for easier usage in docker-compose
33+
# DB_HOST empty means localhost, instead one should set the unix socket and/or other host
34+
DB_HOST = os.environ.get('DJANGO_DB_HOST')
35+
if DB_HOST == None:
36+
DB_HOST = 'docker_db_1'
37+
38+
LOG_LEVEL = os.environ.get('DJANGO_LOG_LEVEL')
39+
if LOG_LEVEL == None:
40+
LOG_LEVEL = 'ERROR'
3041

3142
# Application definition
3243

@@ -72,14 +83,13 @@
7283

7384
WSGI_APPLICATION = 'feedback_plugin.wsgi.application'
7485

75-
7686
DATABASES = {
7787
'default': {
7888
'ENGINE': 'django.db.backends.mysql',
79-
'NAME': os.environ['DJANGO_DB_NAME'],
89+
'NAME': os.environ.get('DJANGO_DB_NAME'),
8090
'USER': os.environ['DJANGO_DB_USER_NAME'],
8191
'PASSWORD': os.environ['DJANGO_DB_USER_PASSWORD'],
82-
'HOST': 'db',
92+
'HOST': DB_HOST,
8393
'OPTIONS': {'charset': 'utf8',
8494
'use_unicode': True},
8595
'TEST': {
@@ -150,12 +160,12 @@
150160
'loggers': {
151161
'django': {
152162
'handlers': ['console'],
153-
'level': os.environ['DJANGO_LOG_LEVEL'],
163+
'level': LOG_LEVEL,
154164
'propagate': False,
155165
},
156166
'views': {
157167
'handlers': ['console'],
158-
'level': os.environ['DJANGO_LOG_LEVEL'],
168+
'level': LOG_LEVEL,
159169
'propagate': False,
160170
},
161171
},

0 commit comments

Comments
 (0)