From 8dd38463547a3f1d8eeb5ef054315a23ef8760f5 Mon Sep 17 00:00:00 2001
From: rlacko <rlacko@sch.bme.hu>
Date: Tue, 4 Feb 2020 18:42:47 +0100
Subject: [PATCH] wait for DB script

---
 Dockerfile        | 10 ++++++++--
 src/entrypoint.sh | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100755 src/entrypoint.sh

diff --git a/Dockerfile b/Dockerfile
index 51c875c..e1e843b 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -10,12 +10,18 @@ ENV PYTHONUNBUFFERED 1
 
 # install dependencies
 RUN apt-get -y update
-RUN apt-get install -y python python-pip python-dev python-django-extensions postgresql-client
+RUN apt-get install -y python python-pip python-dev python-django-extensions postgresql-client netcat
 RUN pip install --upgrade pip
 COPY ./requirements/production.txt /usr/src/app/requirements.txt
 RUN pip install -r requirements.txt
 
 RUN apt-get -y update && apt-get -y autoremove
 
+# copy entrypoint.sh
+COPY ./src/entrypoint.sh /usr/src/app/entrypoint.sh
+
 # copy project
-COPY ./src /usr/src/app/
\ No newline at end of file
+COPY ./src /usr/src/app/
+
+# run entrypoint.sh
+ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
\ No newline at end of file
diff --git a/src/entrypoint.sh b/src/entrypoint.sh
new file mode 100755
index 0000000..08fe051
--- /dev/null
+++ b/src/entrypoint.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+if [ "$DATABASE" = "postgres" ]
+then
+    echo "Waiting for postgres..."
+
+    while ! nc -z $DB_HOST $DB_PORT; do
+      sleep 0.1
+    done
+
+    echo "PostgreSQL started"
+fi
+
+python manage.py flush --no-input
+python manage.py migrate
+
+exec "$@"
\ No newline at end of file
-- 
GitLab