diff --git a/Dockerfile b/Dockerfile index b3f1d6c..825dde7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -72,6 +72,8 @@ COPY ./bin/docker_start.sh /start.sh # COPY ./bin/celery_worker.sh /celery_worker.sh # COPY ./bin/celery_beat.sh /celery_beat.sh # COPY ./bin/celery_flower.sh /celery_flower.sh +COPY ./bin/wait_for_db.sh /wait_for_db.sh +COPY ./bin/setup_configuration.sh /setup_configuration.sh RUN mkdir /app/bin /app/log /app/media VOLUME ["/app/log", "/app/media"] diff --git a/bin/docker_start.sh b/bin/docker_start.sh index bf527b4..bb59ffe 100755 --- a/bin/docker_start.sh +++ b/bin/docker_start.sh @@ -15,12 +15,8 @@ uwsgi_threads=${UWSGI_THREADS:-1} 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 0000000..a133e96 --- /dev/null +++ b/bin/setup_configuration.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# setup initial configuration using a yaml file +# Run this script from the root of the repository + +set -e + +if [[ "${RUN_SETUP_CONFIG,,}" =~ ^(true|1|yes)$ ]]; then + # wait for required services + /wait_for_db.sh + + src/manage.py migrate + src/manage.py setup_configuration --yaml-file setup_configuration/data.yaml +fi diff --git a/bin/wait_for_db.sh b/bin/wait_for_db.sh new file mode 100755 index 0000000..89e15e6 --- /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 09d1cb7..3227a03 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,39 +18,40 @@ services: web: build: . image: maykinmedia/referentielijsten-api:latest - environment: - - DJANGO_SETTINGS_MODULE=referentielijsten.conf.docker - - SECRET_KEY=${SECRET_KEY:-django-insecure-4foe-_dk9x=88*0sljyo1_ga!!nj*x8ye6u0p(@871e)zg^+q} - - DB_NAME=referentielijsten - - DB_USER=referentielijsten - - DB_HOST=db - - CACHE_DEFAULT=redis:6379/0 - - CACHE_AXES=redis:6379/0 - - SUBPATH=${SUBPATH:-/} - - DISABLE_2FA=${DISABLE_2FA:-True} + environment: &app-env + DJANGO_SETTINGS_MODULE: referentielijsten.conf.docker + SECRET_KEY: ${SECRET_KEY:-django-insecure-4foe-_dk9x=88*0sljyo1_ga!!nj*x8ye6u0p(@871e)zg^+q} + DB_NAME: referentielijsten + DB_USER: referentielijsten + DB_HOST: db + CACHE_DEFAULT: redis:6379/0 + CACHE_AXES: redis:6379/0 + SUBPATH: ${SUBPATH:-/} + DISABLE_2FA: ${DISABLE_2FA:-True} # Only allow all hosts for development/testing purposes! - - ALLOWED_HOSTS=* + ALLOWED_HOSTS: '*' ports: - 8000:8000 + volumes: &app-volumes + - media:/app/media # Shared media volume to get access to saved OAS files + - ./docker/setup_configuration:/app/setup_configuration + depends_on: + web-init: + condition: service_completed_successfully + + web-init: + build: . + environment: + <<: *app-env + # + # Django-setup-configuration + RUN_SETUP_CONFIG: ${RUN_SETUP_CONFIG:-true} + command: /setup_configuration.sh depends_on: - db - redis + volumes: *app-volumes -# See: src/referentielijsten/conf/docker.py -# Optional containers below: -# elasticsearch: -# # NOTE: No persistance storage configured. -# # See: https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html -# image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4 -# container_name: elasticsearch -# environment: -# - discovery.type=single-node -# - cluster.routing.allocation.disk.threshold_enabled=false -# ports: -# - 9200:9200 -# redis: -# # NOTE: No persistance storage configured. -# # See: https://hub.docker.com/_/redis/ -# image: redis -# ports: -# - 6379:6379 +volumes: + media: + db: