From d51c89e91fd14fb443f113e1fb885f956ad87663 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 | 22 ++++++++++++++++++++-- 5 files changed, 49 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 3115c308..78b0b3ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,10 +51,12 @@ COPY --from=backend-build /usr/local/bin/celery /usr/local/bin/celery # 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/celery_worker.sh /celery_worker.sh COPY ./bin/celery_flower.sh /celery_flower.sh COPY ./bin/check_celery_worker_liveness.py ./bin/ +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 15511655..16a6df45 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,14 +30,32 @@ services: - CELERY_BROKER_URL=redis://redis:6379/1 - CELERY_RESULT_BACKEND=redis://redis:6379/1 - CELERY_LOGLEVEL=DEBUG + # 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 - - redis + web-init: + condition: service_completed_successfully volumes: &web_volumes - media:/app/media # Shared media volume to get access to saved OAS files + web-init: + build: *web_build + environment: *web_env + command: /setup_configuration.sh + depends_on: + - db + - redis + volumes: *web_volumes + celery: build: *web_build environment: *web_env