From 9109e7e960adbc9b9eb1b36094bdca822c82efae Mon Sep 17 00:00:00 2001 From: Anna Shamray Date: Mon, 11 Mar 2024 17:34:54 +0100 Subject: [PATCH] :whale: [https://github.com/maykinmedia/django-setup-configuration/issues/1] add setup_configuration into docker --- Dockerfile | 2 ++ bin/docker_start.sh | 6 ++---- bin/setup_configuration.sh | 10 ++++++++++ bin/wait_for_db.sh | 15 +++++++++++++++ docker-compose.yml | 21 +++++++++++++++++++-- 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100755 bin/setup_configuration.sh create mode 100755 bin/wait_for_db.sh diff --git a/Dockerfile b/Dockerfile index 2d5e1778..8b851634 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,7 +50,9 @@ COPY --from=backend-build /usr/local/bin/uwsgi /usr/local/bin/uwsgi # Stage 3.2 - Copy source code WORKDIR /app +COPY ./bin/wait_for_db.sh /wait_for_db.sh COPY ./bin/docker_start.sh /start.sh +COPY ./bin/setup_configuration.sh /setup_configuration.sh RUN mkdir /app/log /app/config # copy frontend build statics diff --git a/bin/docker_start.sh b/bin/docker_start.sh index dca77e91..3aa32180 100755 --- a/bin/docker_start.sh +++ b/bin/docker_start.sh @@ -15,10 +15,8 @@ uwsgi_threads=${UWSGI_THREADS:-2} mountpoint=${SUBPATH:-/} -until pg_isready; do - >&2 echo "Waiting for database connection..." - sleep 1 -done +# wait for required services +${SCRIPTPATH}/wait_for_db.sh >&2 echo "Database is up." diff --git a/bin/setup_configuration.sh b/bin/setup_configuration.sh new file mode 100755 index 00000000..716b22e8 --- /dev/null +++ b/bin/setup_configuration.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# setup initial configuration using environment variables +# Run this script from the root of the repository + +#set -e +${SCRIPTPATH}/wait_for_db.sh + +src/manage.py migrate +src/manage.py setup_configuration --no-selftest diff --git a/bin/wait_for_db.sh b/bin/wait_for_db.sh new file mode 100755 index 00000000..89e15e6a --- /dev/null +++ b/bin/wait_for_db.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +set -e + +# Wait for the database container +# See: https://docs.docker.com/compose/startup-order/ +export PGHOST=${DB_HOST:-db} +export PGPORT=${DB_PORT:-5432} + +until pg_isready; do + >&2 echo "Waiting for database connection..." + sleep 1 +done + +>&2 echo "Database is up." diff --git a/docker-compose.yml b/docker-compose.yml index db1a1f51..11e98255 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,19 +11,36 @@ services: web: build: . - environment: + environment: &app-env - DJANGO_SETTINGS_MODULE=objects.conf.docker - SECRET_KEY=${SECRET_KEY:-1(@f(-6s_u(5fd&1sg^uvu2s(c-9sapw)1era8q&)g)h@cwxxg} - OBJECTS_SUPERUSER_USERNAME=admin - OBJECTS_SUPERUSER_PASSWORD=admin - OBJECTS_SUPERUSER_EMAIL=admin@localhost - ALLOWED_HOSTS=* + # setup_configuration env vars + - OBJECTS_DOMAIN=web:8000 + - OBJECTS_ORGANIZATION=Objects + - OBJECTTYPES_API_ROOT=https://objecttypes.example.com/api/v2/ + - OBJECTS_OBJECTTYPES_TOKEN=some-random-string + - DEMO_CONFIG_ENABLE=yes + - DEMO_TOKEN=demo-random-string + - DEMO_PERSON=Demo + - DEMO_EMAIL=demo@demo.local ports: - 8000:8000 depends_on: - - db + web-init: + condition: service_completed_successfully volumes: - media:/app/media # Shared media volume to get access to saved OAS files + web-init: + build: . + environment: *app-env + command: /setup_configuration.sh + depends_on: + - db + volumes: media: