From c9eac83669fbffd76c7a9f5c99699d3f5b10d2b8 Mon Sep 17 00:00:00 2001 From: Martin Ruskov Date: Mon, 4 May 2026 19:13:10 +0200 Subject: [PATCH] Django manage steps in compose startup sequence --- .env.template | 7 ++++-- docker-compose.yml | 25 +++++++++++++++---- .../settings/secret_settings_template.py | 5 ++-- mmt_setup.py | 4 +-- project_static/js/mapHandler.js | 1 + 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.env.template b/.env.template index a3a4269..e62f060 100644 --- a/.env.template +++ b/.env.template @@ -1,4 +1,5 @@ DJANGO_SECRET_KEY= +# space-separated hosts DJANGO_ALLOWED_HOSTS= DJANGO_DB_HOST= DJANGO_DB_NAME= @@ -7,6 +8,8 @@ DJANGO_DB_PASSWORD= DJANGO_SECURE_SSL_HOST=False DJANGO_SESSION_COOKIE_SECURE=False DJANGO_CSRF_COOKIE_SECURE=False -DJANGO_SUPERUSER= +# This is default variable read by createsuperuser --noinput +DJANGO_SUPERUSER_USERNAME= DJANGO_SUPERUSER_PASSWORD= -HTTP_X_FORWARDED_PROTO= \ No newline at end of file +DJANGO_SUPERUSER_EMAIL= +HTTP_X_FORWARDED_PROTO= diff --git a/docker-compose.yml b/docker-compose.yml index 3f61a49..801a1b9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.4' - # For use in development only. Starts two containers - one for MMT, one for postgres # with volumes for the /app directory and the postgres data. /app dir mapped means that # changes to the code reloads MMT. @@ -20,6 +18,11 @@ services: env_file: - .env platform: linux/amd64 + healthcheck: + test: ["CMD-SHELL", "pg_isready --username=${DJANGO_DB_USER} --dbname=${DJANGO_DB_NAME}"] + start_period: 10s + restart: always + memorymaptoolkit: image: memorymaptoolkit build: @@ -31,8 +34,15 @@ services: - backups:/app/backups - .:/app depends_on: - - db - command: python manage.py runserver 0.0.0.0:8000 --settings=memorymap_toolkit.settings.local + db: + condition: service_healthy + command: > + bash -c " + python manage.py migrate && \ + (python manage.py createsuperuser --noinput || echo 'Assuming preexisting user and continuing...') && \ + python manage.py collectstatic --noinput && \ + python manage.py runserver 0.0.0.0:8000 + " ports: - 8000:8000 env_file: @@ -47,8 +57,13 @@ services: - DJANGO_SESSION_COOKIE_SECURE=${DJANGO_SESSION_COOKIE_SECURE} - DJANGO_CSRF_COOKIE_SECURE=${DJANGO_CSRF_COOKIE_SECURE} - DJANGO_DB_HOST=${DJANGO_DB_HOST} + - DJANGO_SETTINGS_MODULE=memorymap_toolkit.settings.local + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000')"] + restart: always + volumes: postgres_data: static: media: - backups: \ No newline at end of file + backups: diff --git a/memorymap_toolkit/settings/secret_settings_template.py b/memorymap_toolkit/settings/secret_settings_template.py index 3d60cb0..40fdfcb 100644 --- a/memorymap_toolkit/settings/secret_settings_template.py +++ b/memorymap_toolkit/settings/secret_settings_template.py @@ -3,7 +3,8 @@ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', '') -ALLOWED_HOSTS = [os.environ.get('DJANGO_ALLOWED_HOSTS', 'localhost')] +# Hosts are split in case more than one (space-separated) host is provided in the .env file +ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', 'localhost').split() # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases @@ -29,4 +30,4 @@ # SESSION_COOKIE_SECURE = True -# CSRF_COOKIE_SECURE = True \ No newline at end of file +# CSRF_COOKIE_SECURE = True diff --git a/mmt_setup.py b/mmt_setup.py index 88af869..7856709 100644 --- a/mmt_setup.py +++ b/mmt_setup.py @@ -34,6 +34,6 @@ except: pw = os.environ.get('DJANGO_SUPERUSER_PASSWORD') if pw: - u = User.objects.create(username=os.environ.get('DJANGO_SUPERUSER', 'admin'), is_superuser=True, is_staff=True) + u = User.objects.create(username=os.environ.get('DJANGO_SUPERUSER_USERNAME', 'admin'), is_superuser=True, is_staff=True) u.set_password(pw) - u.save() \ No newline at end of file + u.save() diff --git a/project_static/js/mapHandler.js b/project_static/js/mapHandler.js index 440c2ec..37edc38 100644 --- a/project_static/js/mapHandler.js +++ b/project_static/js/mapHandler.js @@ -307,6 +307,7 @@ map.on('load', function() { ] } }); + });