From 7fd30a5dcd752995d00ecfe17ee0203ebdf224c4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 25 Aug 2022 11:03:25 +0530 Subject: [PATCH] Add production ready Dockerfle and entrypoint files --- Dockerfile.prod | 71 ++++++++++++++++++++++++++++++++++++++++++++++ entrypoint.prod.sh | 15 ++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Dockerfile.prod create mode 100755 entrypoint.prod.sh diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000..9b51ad7 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,71 @@ +########### +# BUILDER # +########### + +# pull official base image +FROM python:3.9.6-alpine as builder + +# set work directory +WORKDIR /usr/src/gmba_django + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# install psycopg2 dependencies +RUN apk update \ + && apk add postgresql-dev gcc python3-dev musl-dev + +# lint +RUN pip install --upgrade pip +RUN pip install flake8==3.9.2 +COPY . . +RUN flake8 --ignore=E501,F401 . + +# install dependencies +COPY ./requirements.txt . +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/gmba_django/wheels -r requirements.txt + + +######### +# FINAL # +######### + +# pull official base image +FROM python:3.9.6-alpine + +# create directory for the app user +RUN mkdir -p /home/app + +# create the app user +RUN addgroup -S app && adduser -S app -G app + +# create the appropriate directories +ENV HOME=/home/app +ENV APP_HOME=/home/app/app +RUN mkdir $APP_HOME +WORKDIR $APP_HOME + +# install dependencies +RUN apk update && apk add libpq +COPY --from=builder /usr/src/gmba_django/wheels /wheels +COPY --from=builder /usr/src/gmba_django/requirements.txt . +RUN pip install --no-cache /wheels/* + +# copy entrypoint.prod.sh +COPY ./entrypoint.prod.sh . +RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh +RUN chmod +x $APP_HOME/entrypoint.prod.sh + +# copy project +COPY . $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.prod.sh +ENTRYPOINT ["/home/app/app/entrypoint.prod.sh"] + diff --git a/entrypoint.prod.sh b/entrypoint.prod.sh new file mode 100755 index 0000000..2cc61cd --- /dev/null +++ b/entrypoint.prod.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +if [ "$DATABASE" = "postgres" ] +then + echo "Waiting for postgres..." + + while ! nc -z $SQL_HOST $SQL_PORT; do + sleep 0.1 + done + + echo "PostgreSQL started" +fi + +exec "$@" +