diff --git a/Dockerfile b/Dockerfile index e1e843b03a368c0afc82e41381d1a27de34c0b73..0dd2d90d20d02e6e18f3773fa43b4feeea582145 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # pull official base image -FROM python:3.8.0 +FROM python:3.8.1 # set work directory WORKDIR /usr/src/app diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000000000000000000000000000000000000..ef6ac28c9a0c80a1111b0d3b1d34b6ad58965f6c --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,69 @@ +########### +# BUILDER # +########### + +# pull official base image +FROM python:3.8.1 as builder + +# set work directory +WORKDIR /usr/src/app + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +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 netcat +RUN apt-get -y update && apt-get -y autoremove + +# lint +RUN pip install --upgrade pip +RUN pip install flake8 +COPY ./src /usr/src/app/ +#RUN flake8 --max-line-length=125 --exclude=kszkepzes,migrations . + +# install dependencies +COPY ./requirements/production.txt /usr/src/app/requirements.txt +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt + +######### +# FINAL # +######### + +# pull official base image +FROM python:3.8.1 + +# create directory for the app user +RUN mkdir -p /home/app + +# create the app user +RUN groupadd app && useradd -g app app + +# create the appropriate directories +ENV HOME=/home/app +ENV APP_HOME=/home/app/kszkepzes-backend +RUN mkdir $APP_HOME +WORKDIR $APP_HOME + +# install dependencies +RUN apt-get -y update && apt-get install netcat -y +COPY --from=builder /usr/src/app/wheels /wheels +COPY --from=builder /usr/src/app/requirements.txt . +RUN pip install --upgrade pip +RUN pip install --no-cache /wheels/* + +# copy entrypoint-prod.sh +COPY ./src/entrypoint.prod.sh $APP_HOME + +# copy project +COPY ./src $APP_HOME + +# chown all the files to the app user +RUN chown -R app:app $APP_HOME + +# change to the app user +USER app + +# run entrypoint.sh +ENTRYPOINT ["/home/app/kszkepzes-backend/entrypoint.prod.sh"] \ No newline at end of file diff --git a/src/entrypoint.prod.sh b/src/entrypoint.prod.sh new file mode 100755 index 0000000000000000000000000000000000000000..5236bfaeabe748c12cfce8f387009f568021975a --- /dev/null +++ b/src/entrypoint.prod.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +if [ "$DJANGO_SETTINGS_MODULE" = "kszkepzes.settings.production" ] +then + echo "Waiting for postgres..." + + while ! nc -z $DB_HOST $DB_PORT; do + sleep 0.1 + done + + echo "PostgreSQL started" +fi + +exec "$@" \ No newline at end of file diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 08fe051b9daffce26a731f3e710e4455abf047ca..84c451504517e8c4ae40f110bbd48b8ce4d5fd20 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -1,6 +1,6 @@ #!/bin/sh -if [ "$DATABASE" = "postgres" ] +if [ "$DJANGO_SETTINGS_MODULE" = "kszkepzes.settings.production" ] then echo "Waiting for postgres..." diff --git a/src/kszkepzes/settings/production.py b/src/kszkepzes/settings/production.py index 7c121b50db4caa8c0c3823507663e1dc3aadf673..785c9826aa58dead2370974d16eaf7e0fdd3781f 100644 --- a/src/kszkepzes/settings/production.py +++ b/src/kszkepzes/settings/production.py @@ -6,7 +6,7 @@ ALLOWED_HOSTS = ['*'] DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.postgresql', + 'ENGINE': os.getenv('DB_ENGINE', 'django.db.backends.postgresql'), 'NAME': os.getenv('DB_NAME', 'kszkepzes'), 'USER': os.getenv('DB_USER'), 'PASSWORD': os.getenv('DB_PASSWORD'), diff --git a/src/manage.py b/src/manage.py index 57605e12a663a66926941435778a458fbdcdc160..e4402b7b44c4dde5fabcac097975a806d90b055f 100755 --- a/src/manage.py +++ b/src/manage.py @@ -3,7 +3,7 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kszkepzes.settings.production") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kszkepzes.settings.local") try: from django.core.management import execute_from_command_line except ImportError: