From 34631ad819ba63ba2dccebb4c867838371731c7a Mon Sep 17 00:00:00 2001 From: Jannis Gebauer Date: Tue, 16 Aug 2016 21:20:41 +0200 Subject: [PATCH] adds the ability to wait for the postgres container (#736) --- .../compose/django/Dockerfile-dev | 4 ++++ .../compose/django/entrypoint.sh | 23 ++++++++++++++++++- .../compose/django/start-dev.sh | 3 +++ {{cookiecutter.project_slug}}/dev.yml | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/compose/django/start-dev.sh diff --git a/{{cookiecutter.project_slug}}/compose/django/Dockerfile-dev b/{{cookiecutter.project_slug}}/compose/django/Dockerfile-dev index c3f54a9e..5d0631d5 100644 --- a/{{cookiecutter.project_slug}}/compose/django/Dockerfile-dev +++ b/{{cookiecutter.project_slug}}/compose/django/Dockerfile-dev @@ -13,6 +13,10 @@ COPY ./compose/django/entrypoint.sh /entrypoint.sh RUN sed -i 's/\r//' /entrypoint.sh RUN chmod +x /entrypoint.sh +COPY ./compose/django/start-dev.sh /start-dev.sh +RUN sed -i 's/\r//' /start-dev.sh +RUN chmod +x /start-dev.sh + WORKDIR /app ENTRYPOINT ["/entrypoint.sh"] diff --git a/{{cookiecutter.project_slug}}/compose/django/entrypoint.sh b/{{cookiecutter.project_slug}}/compose/django/entrypoint.sh index e9286baa..9fe7f5c8 100644 --- a/{{cookiecutter.project_slug}}/compose/django/entrypoint.sh +++ b/{{cookiecutter.project_slug}}/compose/django/entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/bash set -e +cmd="$@" + # This entrypoint is used to play nicely with the current cookiecutter configuration. # Since docker-compose relies heavily on environment variables itself for configuration, we'd have to define multiple # environment variables just to support cookiecutter out of the box. That makes no sense, so this little entrypoint @@ -15,4 +17,23 @@ export DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@postgres:5432/$ {% if cookiecutter.use_celery == 'y' %} export CELERY_BROKER_URL=$REDIS_URL/0 {% endif %} -exec "$@" + +function postgres_ready(){ +python << END +import sys +import psycopg2 +try: + conn = psycopg2.connect(dbname="$POSTGRES_USER", user="$POSTGRES_USER", password="$POSTGRES_PASSWORD", host="postgres") +except psycopg2.OperationalError: + sys.exit(-1) +sys.exit(0) +END +} + +until postgres_ready; do + >&2 echo "Postgres is unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up - continuing..." +exec $cmd diff --git a/{{cookiecutter.project_slug}}/compose/django/start-dev.sh b/{{cookiecutter.project_slug}}/compose/django/start-dev.sh new file mode 100644 index 00000000..04e06981 --- /dev/null +++ b/{{cookiecutter.project_slug}}/compose/django/start-dev.sh @@ -0,0 +1,3 @@ +#!/bin/sh +python manage.py migrate +python manage.py runserver_plus 0.0.0.0:8000 diff --git a/{{cookiecutter.project_slug}}/dev.yml b/{{cookiecutter.project_slug}}/dev.yml index 35282fe6..3eabb901 100644 --- a/{{cookiecutter.project_slug}}/dev.yml +++ b/{{cookiecutter.project_slug}}/dev.yml @@ -17,7 +17,7 @@ services: build: context: . dockerfile: ./compose/django/Dockerfile-dev - command: python /app/manage.py runserver_plus 0.0.0.0:8000 + command: /start-dev.sh depends_on: - postgres environment: