From 5ec904631cb4742fce4f33bcd2a40bec7bf8f5a5 Mon Sep 17 00:00:00 2001 From: Anna Shamray Date: Tue, 5 Mar 2024 17:14:24 +0100 Subject: [PATCH] :whale: [https://github.com/maykinmedia/django-setup-configuration/issues/1] add setup_configuration into docker --- Dockerfile | 3 +++ bin/docker_start.sh | 8 ++------ bin/setup_configuration.sh | 11 +++++++++++ bin/wait_for_db.sh | 15 +++++++++++++++ docker-compose.yml | 31 ++++++++++++++++++++++++------- docker-init-db.sql | 3 +++ setup.cfg | 1 - 7 files changed, 58 insertions(+), 14 deletions(-) create mode 100755 bin/setup_configuration.sh create mode 100755 bin/wait_for_db.sh create mode 100644 docker-init-db.sql diff --git a/Dockerfile b/Dockerfile index d9ec8f61..18d541c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ pkg-config \ build-essential \ libpq-dev \ + git \ && rm -rf /var/lib/apt/lists/* WORKDIR /app @@ -54,7 +55,9 @@ COPY --from=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 --from=frontend-build /app/src/objecttypes/static /app/src/objecttypes/static diff --git a/bin/docker_start.sh b/bin/docker_start.sh index 2516c4dd..0328cec3 100755 --- a/bin/docker_start.sh +++ b/bin/docker_start.sh @@ -15,12 +15,8 @@ uwsgi_threads=${UWSGI_THREADS:-2} mountpoint=${SUBPATH:-/} -until pg_isready; do - >&2 echo "Waiting for database connection..." - sleep 1 -done - ->&2 echo "Database is up." +# wait for required services +${SCRIPTPATH}/wait_for_db.sh # Apply database migrations >&2 echo "Apply database migrations" diff --git a/bin/setup_configuration.sh b/bin/setup_configuration.sh new file mode 100755 index 00000000..216f8697 --- /dev/null +++ b/bin/setup_configuration.sh @@ -0,0 +1,11 @@ +#!/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 b08354f0..90561482 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,20 +2,37 @@ version: '3' services: db: - # NOTE: No persistance storage configured. - # See: https://hub.docker.com/_/postgres/ - image: postgres + image: postgres:11-alpine environment: - - POSTGRES_USER=${DB_USER:-objecttypes} - - POSTGRES_PASSWORD=${DB_PASSWORD:-objecttypes} + - POSTGRES_HOST_AUTH_METHOD=trust + volumes: + - ./docker-init-db.sql:/docker-entrypoint-initdb.d/init_db.sql +# - db:/var/lib/postgresql/data + command: postgres -c max_connections=300 -c log_min_messages=LOG web: build: . - environment: + environment: &app-env - DJANGO_SETTINGS_MODULE=objecttypes.conf.docker - SECRET_KEY=${SECRET_KEY:-fgv=c0hz&tl*8*3m3893@m+1pstrvidc9e^5@fpspmg%cy$15d} - ALLOWED_HOSTS=* + - TWO_FACTOR_FORCE_OTP_ADMIN=no + - TWO_FACTOR_PATCH_ADMIN=no + # setup_configuration env vars + - OBJECTTYPES_DOMAIN=web:8000 + - OBJECTTYPES_ORGANIZATION=ObjectTypes + - OBJECTS_OBJECTTYPES_TOKEN=some-random-string + - OBJECTS_OBJECTTYPES_PERSON=Some Person + - OBJECTS_OBJECTTYPES_EMAIL=objects@objects.local ports: - 8000:8000 depends_on: - - db + web-init: + condition: service_completed_successfully + + web-init: + build: . + environment: *app-env + command: /setup_configuration.sh + depends_on: + - db \ No newline at end of file diff --git a/docker-init-db.sql b/docker-init-db.sql new file mode 100644 index 00000000..7e5eb4df --- /dev/null +++ b/docker-init-db.sql @@ -0,0 +1,3 @@ +CREATE USER objecttypes; +CREATE DATABASE objecttypes; +GRANT ALL PRIVILEGES ON DATABASE objecttypes TO objecttypes; diff --git a/setup.cfg b/setup.cfg index cf5b7f91..6e2f763e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,7 +12,6 @@ line_length = 88 multi_line_output = 3 skip = env,node_modules skip_glob = **/migrations/** -not_skip = __init__.py known_django=django known_first_party=objecttypes sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER