From a35aa953b4fb9949348e1c16bd498b5e2b876e07 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:15:04 +0100 Subject: [PATCH 1/4] Ping DB instead of trying migration --- Dockerfile | 2 +- boot.sh | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index b69c037ac4..68d2366dad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ WORKDIR /opt/recipes COPY requirements.txt ./ -RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev zlib-dev jpeg-dev libwebp-dev libressl-dev libffi-dev cargo openssl-dev openldap-dev && \ +RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev postgresql zlib-dev jpeg-dev libwebp-dev libressl-dev libffi-dev cargo openssl-dev openldap-dev && \ python -m venv venv && \ /opt/recipes/venv/bin/python -m pip install --upgrade pip && \ venv/bin/pip install wheel==0.36.2 && \ diff --git a/boot.sh b/boot.sh index 7e2f05fa09..ced7aaa29b 100644 --- a/boot.sh +++ b/boot.sh @@ -1,28 +1,28 @@ #!/bin/sh source venv/bin/activate -echo "Migrating database" +echo "Waiting for database to be ready..." attempt=0 max_attempts=20 -while python manage.py migrate; \ - status=$?; \ - attempt=$((attempt+1)); \ - [ $status -eq 1 ] \ - && [ $attempt -le $max_attempts ]; do - echo -e "\n!!! Migration failed (error ${status}, attempt ${attempt}/${max_attempts})." - echo "!!! Database may not be ready yet or system is misconfigured." - echo -e "!!! Retrying in 5 seconds...\n" - sleep 5 +while pg_isready --host=${POSTGRES_HOST} -q; status=$?; attempt=$((attempt+1)); [ $status -ne 0 ] && [ $attempt -le $max_attempts ]; do + sleep 5 # no echo needed, response comes from pg_isready already done if [ $attempt -gt $max_attempts ]; then - echo -e "\n!!! Migration failed. Maximum attempts exceeded." - echo "!!! Please check logs above - misconfiguration is very likely." - echo "!!! Shutting down container." + echo -e "\nDatabase not reachable. Maximum attempts exceeded." + echo "Please check logs above - misconfiguration is very likely." + echo "Make sure the DB container is up and POSTGRES_HOST is set properly." + echo "Shutting down container." exit 1 # exit with error to make the container stop fi +echo "Database is ready" + +echo "Migrating database" + +python manage.py migrate + echo "Generating static files" python manage.py collectstatic_js_reverse @@ -32,4 +32,4 @@ echo "Done" chmod -R 755 /opt/recipes/mediafiles -exec gunicorn -b :8080 --access-logfile - --error-logfile - --log-level INFO recipes.wsgi +exec gunicorn -b :8080 --access-logfile - --error-logfile - --log-level INFO recipes.wsgi \ No newline at end of file From cb755a47bcf8986fa53bc9a4db04046efc1bc3f6 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:19:32 +0100 Subject: [PATCH 2/4] Remove wrong code comment --- boot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boot.sh b/boot.sh index ced7aaa29b..9e081d5b5f 100644 --- a/boot.sh +++ b/boot.sh @@ -6,7 +6,7 @@ echo "Waiting for database to be ready..." attempt=0 max_attempts=20 while pg_isready --host=${POSTGRES_HOST} -q; status=$?; attempt=$((attempt+1)); [ $status -ne 0 ] && [ $attempt -le $max_attempts ]; do - sleep 5 # no echo needed, response comes from pg_isready already + sleep 5 done if [ $attempt -gt $max_attempts ]; then @@ -32,4 +32,4 @@ echo "Done" chmod -R 755 /opt/recipes/mediafiles -exec gunicorn -b :8080 --access-logfile - --error-logfile - --log-level INFO recipes.wsgi \ No newline at end of file +exec gunicorn -b :8080 --access-logfile - --error-logfile - --log-level INFO recipes.wsgi From 45942dfa7f9a8f81948364dcab546990f0cc77e5 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Tue, 8 Feb 2022 18:20:19 +0100 Subject: [PATCH 3/4] Fixed indentation --- boot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boot.sh b/boot.sh index 9e081d5b5f..187e4c9514 100644 --- a/boot.sh +++ b/boot.sh @@ -12,7 +12,7 @@ done if [ $attempt -gt $max_attempts ]; then echo -e "\nDatabase not reachable. Maximum attempts exceeded." echo "Please check logs above - misconfiguration is very likely." - echo "Make sure the DB container is up and POSTGRES_HOST is set properly." + echo "Make sure the DB container is up and POSTGRES_HOST is set properly." echo "Shutting down container." exit 1 # exit with error to make the container stop fi From 2471bb21f65b0e93f383b08d5b83c7312c965fd0 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Tue, 8 Feb 2022 19:00:54 +0100 Subject: [PATCH 4/4] Fixed dep. position and used smaller package --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 68d2366dad..5e9fb621d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM python:3.9-alpine3.12 #Install all dependencies. -RUN apk add --no-cache postgresql-libs gettext zlib libjpeg libwebp libxml2-dev libxslt-dev py-cryptography +RUN apk add --no-cache postgresql-libs postgresql-client gettext zlib libjpeg libwebp libxml2-dev libxslt-dev py-cryptography #Print all logs without buffering it. ENV PYTHONUNBUFFERED 1 @@ -15,7 +15,7 @@ WORKDIR /opt/recipes COPY requirements.txt ./ -RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev postgresql zlib-dev jpeg-dev libwebp-dev libressl-dev libffi-dev cargo openssl-dev openldap-dev && \ +RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev zlib-dev jpeg-dev libwebp-dev libressl-dev libffi-dev cargo openssl-dev openldap-dev && \ python -m venv venv && \ /opt/recipes/venv/bin/python -m pip install --upgrade pip && \ venv/bin/pip install wheel==0.36.2 && \